TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25099 16
Sub-microsecond timing on EC2 is way messier than I expected

Been doing sub-microsecond profiling on EC2 and kept getting wildly inconsistent cycle counts.

One mistake was using cpuid as the serialization barrier before rdtsc. On a VM that can be a mess, since cpuid often traps so the hypervisor can fake feature flags. So now the "measurement overhead" includes a VM exit, which is thousands of cycles on some runs.

Switching to lfence + rdtsc made the numbers a lot more stable.

Then I hit the calibration problem. Measuring TSC frequency with a short sleep() looked simple, but the results were all over the place. Scheduler delay, timer granularity, and probably vCPU steal time were enough to make the calibration useless at this scale. A busy-wait loop with pause gave me a much saner number.

Also forgot to pin the thread at first. rdtscp at least tells you when you migrated, but those samples are basically trash. Same with the first few iterations before icache/branch predictor warm up.

Curious what people here actually use for sub-microsecond timing. Do you just trust nanobench / Google Benchmark, or do you still end up writing your own rdtsc wrappers once VMs get involved?

https://redd.it/1t0o16n
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 4, 2026PJ Plauger's final column at C/C++ Users Journal (2000) https://jacobfilipp.com/DrDobbs/ar…
  2. Oct 4, 2026Why C++ is so hated!!! I love C++. High level + low level at the same time. But Linus Torv…
  3. Oct 4, 2026About alignment, struct layout and the cache... https://meetingcpp.com/blog/items/About-al…
  4. Oct 4, 20260x40: Intro, NB and C++ news, the 10 year anniversary , and the Quiz https://youtu.be/cEjc…
  5. Oct 4, 2026Packing Binary Is Fun, Actually https://hereticpleb.vercel.app/blog/packing-binary-is-fun-…
  6. Oct 3, 2026I've tried "Token Sequence Injection & Modern Macros" and here are my thoughts We had this…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →