Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:54:03+00:00 2026-05-23T16:54:03+00:00

I am writing a c++ benchmarking program, which involves timing a number of function

  • 0

I am writing a c++ benchmarking program, which involves timing a number of function calls. The functions are called repeatedly and each time is recorded for statistical analysis later. It is required that the functions be run simultaneously on multiple threads and thus to ensure accuracy and fairness of the benchmark, it is run on a real-time OS, with the scheduling behavior being controlled. The following are my concerns:

Are there deterministic ways of collecting the timing data? I have looked at printf and stringstream but neither seems to have deterministic behavior due to memory & buffer operations. They also do not perform in O(1) for the same reason, am I right? Currently I am using a large char array and a custom strcat function so that each time value can be collected in O(1). This array is then printed at the end of the test, when all data has been collected.

I am using clock_gettime for timings and clock_getres gives me a resolution of 1ns. Can this value be trusted?

Am I doing things right so far, and are there any other issues that I should be aware of when writing the benchmark?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:54:04+00:00Added an answer on May 23, 2026 at 4:54 pm

    Calling high-frequency timers and writing samples into an output stream is a perfectly sensible way to get performance data. But there are a few tricky gotchas to be careful of.

    • Indeed you shouldn’t use printf and stringstream — not only because their execution time is variable and poorly defined, but also because they’re just darn slow, especially if you’re formatting your perf data into strings every microsecond! It’s much better to write binary data into a preallocated buffer, like an array of structures, and then format them later after your test is done. That will be faster and give you a more consistent write overhead.
    • clock_gettime with the high-resolution timer (eg CLOCK_PROCESS_CPUTIME_ID) should be reliable if the person who wrote your kernel wasn’t a dunce. You can look into the Performance Application Programming Interface library if you want to query the CPU timers directly, but that shouldn’t be necessary.
    • Multithreading can be inherently chaotic (in the determinism sense) because the threads are fighting each other for CPU cache and memory bandwidth. You can get stochastically varying results depending on whether simultaneously scheduled threads happen to be touching the same memory, or are evicting each other’s work from data caches all the time — and this will vary from run to run depending on exactly how the data is laid out in memory and which threads are running. But that’s fine: lots of processes in engineering are stochastic. Just run your benchmark many times and get a statistically significant average and deviation for your perf numbers.

    Or, if you truly need to have 100% determinism, you’ll need to ensure that your threads schedule in the same order, run for the same quanta, and put their data in the same memory addresses for each run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small benchmarking library in C which is used to benchmark single
I've been benchmarking the performance of a framework I'm writing in Perl and I'm
Writing a python program, and I came up with this error while using the
I am interested in writing a program that calculates the value of Pi to
Writing some docs with code snippets which I want to be copyable to run
Writing a templated function, I declared: template <typename T> T invertible(T const& container, T::size_type
Writing an app which takes a picture from the camera and retrieves it as
Writing template tags isn't easy in Django and involves lots of boilerplate code. What
I'm writing some micro-benchmarking code for some very short operations in C. For example,
Writing a simple program that will find exact duplicate files on my computer, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.