---
title: "Profiler"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/profiler/nodes/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Profiler

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/profiler/nodes/

This sample times two ways of sorting with [profiler](https://developer.repebble.com/pebbleos/docs/subsystems/profiler.md) nodes. Each node, defined with `PBL_PROFILER_NODE_DEFINE()`, accumulates the CPU cycles spent between its start and stop, however many times it runs; the sample shuffles the values before each sort under a node of its own.

`pbl_profiler_print_stats()` logs a line per node, the busiest first, and the `PBL_PROFILER_NODE_*` macros read a node’s totals.

Run:

`pbl samples samples/subsys/profiler/nodes`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/profiler/profiler.h\>#define VALUES 128U#define ROUNDS 20U/\* Each node accumulates the cycles spent between its start and its stop. \*/PBL\_PROFILER\_NODE\_DEFINE(bubble\_sort);PBL\_PROFILER\_NODE\_DEFINE(insertion\_sort);PBL\_PROFILER\_NODE\_DEFINE(shuffle);static uint16\_t s\_values[VALUES];static void prv\_shuffle(uint32\_t seed) {for (unsigned int i = 0; i \< VALUES; i++) {seed = seed \* 1664525U + 1013904223U;s\_values[i] = (uint16\_t)(seed \>\> 16);}}static void prv\_bubble\_sort(void) {for (unsigned int i = 0; i \< VALUES - 1U; i++) {for (unsigned int j = 0; j \< VALUES - 1U - i; j++) {if (s\_values[j] \> s\_values[j + 1U]) {uint16\_t tmp = s\_values[j];s\_values[j] = s\_values[j + 1U];s\_values[j + 1U] = tmp;}}}}static void prv\_insertion\_sort(void) {for (unsigned int i = 1; i \< VALUES; i++) {uint16\_t val = s\_values[i];unsigned int j = i;while (j \> 0 && s\_values[j - 1U] \> val) {s\_values[j] = s\_values[j - 1U];j--;}s\_values[j] = val;}}int main(void) {pbl\_profiler\_reset();pbl\_profiler\_start();for (unsigned int round = 0; round \< ROUNDS; round++) {PBL\_PROFILER\_NODE\_START(shuffle);prv\_shuffle(round);PBL\_PROFILER\_NODE\_STOP(shuffle);PBL\_PROFILER\_NODE\_START(bubble\_sort);prv\_bubble\_sort();PBL\_PROFILER\_NODE\_STOP(bubble\_sort);PBL\_PROFILER\_NODE\_START(shuffle);prv\_shuffle(round);PBL\_PROFILER\_NODE\_STOP(shuffle);PBL\_PROFILER\_NODE\_START(insertion\_sort);prv\_insertion\_sort();PBL\_PROFILER\_NODE\_STOP(insertion\_sort);}/\* Stops the run and logs a line per node, the busiest first. \*/pbl\_profiler\_print\_stats();printf("profiler: %u sorts of %u values each way\n",(unsigned int)PBL\_PROFILER\_NODE\_COUNT(bubble\_sort), VALUES);printf("profiler: bubble sort %u us, insertion sort %u us\n",(unsigned int)PBL\_PROFILER\_NODE\_TOTAL\_US(bubble\_sort),(unsigned int)PBL\_PROFILER\_NODE\_TOTAL\_US(insertion\_sort));uint32\_t bubble = PBL\_PROFILER\_NODE\_TOTAL\_CYCLES(bubble\_sort);uint32\_t insertion = PBL\_PROFILER\_NODE\_TOTAL\_CYCLES(insertion\_sort);printf("profiler: insertion sort took fewer cycles: %s\n", insertion \< bubble ? "yes" : "no");printf("Sample done\n");return 0;}
```

## Output

```
Ran for \d+ cycles \(\d+ us\) at \d+ Hz
Name +Count +Cycles +Time \(us\) +Avg \(us\) +% CPU
bubble_sort +20 
(insertion_sort +20|shuffle +40) 
(insertion_sort +20|shuffle +40) 
profiler: 20 sorts of 128 values each way
profiler: bubble sort \d+ us, insertion sort \d+ us
profiler: insertion sort took fewer cycles: yes
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/subsys/profiler/nodes.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/subsys/profiler/nodes.md).
