Different execution orders cause differences in performance of a Pthread program Different execution orders cause differences in performance of a Pthread program unix unix

Different execution orders cause differences in performance of a Pthread program


In a comment, you wrote:

The processor is IntelĀ® Celeron(R) CPU 420 @ 1.60GHz. One core.

Since you only have one core, your thread executions are serialized anyway. Your program with two threads running concurrently pays the overhead of thread context switching as each performs blocking I/O.

When you launch the second thread after the first thread completes it's execution, there is no context switching overhead.


Try to do the same measure but run your program a 100 times and calculate the average time, with such a short time the effect of caching is far from neglictible for an example.


How did you measure?

Real time is not an indication of how long your program ran. You have to measure user+system time. What's more, meaningful timing at the millisecond level depends very much on the granularity of your timing clock. If it runs, say, at 60Hz, then you have a problem.Coming up with meaningful benchmarks is an art...

As a start, you should find a way to run your threads in a loop, say, 10.000 times and add up numbers. That'll at least get you out of the millisecond timing problem.