Skip to content

Instantly share code, notes, and snippets.

@yuygfgg
Created March 21, 2026 03:47
Show Gist options
  • Select an option

  • Save yuygfgg/46410eeee8a1d523d38403774547e32c to your computer and use it in GitHub Desktop.

Select an option

Save yuygfgg/46410eeee8a1d523d38403774547e32c to your computer and use it in GitHub Desktop.
Test the true CPU frequency on Arm VMs
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#define A1 "add %0, %0, #1\n\t"
#define A10 A1 A1 A1 A1 A1 A1 A1 A1 A1 A1
#define A100 A10 A10 A10 A10 A10 A10 A10 A10 A10 A10
#define A1000 A100 A100 A100 A100 A100 A100 A100 A100 A100 A100
double get_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec * 1e-9;
}
int main() {
uint64_t loops = 10000000;
uint64_t insns_per_loop = 1000;
uint64_t total_cycles = loops * insns_per_loop;
uint64_t dummy = 0;
double start_time = get_time();
__asm__ __volatile__(
"1:\n\t"
A1000
"subs %1, %1, #1\n\t"
"b.ne 1b\n\t"
: "+r" (dummy), "+r" (loops)
:
: "cc"
);
double end_time = get_time();
double time_spent = end_time - start_time;
double freq_Hz = (double)total_cycles / time_spent;
double freq_GHz = freq_Hz / 1e9;
printf("%.3f GHz\n", freq_GHz);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment