0

I'm using this method: Measure execution time in C (on Windows) to measure the execution time of all threads. I'm on Windows and I'm using VC++.

Something like this:

//initialize start, end, frequency
std::thread thread1(...); 
// and so on I initialise all the threads

thread1.join();
//join all threads

//time = (start-end)/frequency
printf(time);

I run it multiple times and I often get different execution time. I have like 30% variation.(which means I get values between 100s and 70s). I don't understand why ? 30% is not I small variation.

Thanks

Community
  • 1
  • 1
userDS
  • 15
  • 5
  • 1
    How do you measure time? Consider measuring processor time if you're measuring astronomical since the latter depends on the system load – alexeykuzmin0 Aug 17 '16 at 11:12
  • Time measurement resolution is limited by scheduling and other factors. The shorter the thread run time, the more inaccurate your timing will be. I would take an average of many runs. – Robinson Aug 17 '16 at 11:12
  • @alexeykuzmin0 same as in the answer of the question I linked I copy paste. Using QueryPerformanceCounter. – userDS Aug 17 '16 at 11:15
  • You're measuring "wall-clock" time, not the time spent in your own process. – molbdnilo Aug 17 '16 at 11:22
  • @molbdnilo How should I measure the time spent in my own process ? – userDS Aug 17 '16 at 11:26
  • 30% variation is not a lot, but a variation from 70 seconds to 100 seconds is gigantic. It means there is room from improvement in whatever thread1 is doing. – UmNyobe Aug 18 '16 at 09:51

2 Answers2

0

Many things can affect an execution time, from system load to memory usage to the stars aligning. You don't specify the order of your execution time, but for lower values a 30% deviation is certainly not unheard of. The main take away is that an execution time on its own basically tells us nothing about the system.

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
  • so what your saying is the there is no way I could no exactly how much time it takes to execute my program ? Because in my case it's really important to know the exact execution time. Thanks – userDS Aug 17 '16 at 11:19
  • 1
    There's no such thing as an "exact execution time" in the general case. You can do multiple runs and take an average, or you can take the time of a single run, but there's no way to say "This program will take exactly n seconds to run" in the general case. – Sinkingpoint Aug 17 '16 at 11:23
  • @userDS *exact execution time* is the property of what you call real time operating systems. Windows is not one of them. That said a 30s variation is questionable, except if you access slow resources like disk or network. So investigate. there is something fishy. – UmNyobe Aug 18 '16 at 09:48
  • @UmNyobe I write to the disk every time. – userDS Aug 18 '16 at 09:55
  • @userDS there you have it. you need all the thread to send data to a single thread which is charge of writing (multiple producer, single consumer). see http://stackoverflow.com/questions/10254147/writing-data-to-disk-in-parallel – UmNyobe Aug 18 '16 at 09:57
  • @UmNyobe you are right thanks. Is there anything that I can do to improve my performance. For ex have one more disk, or write to an external disk, anything ? – userDS Aug 18 '16 at 10:02
  • @userDS multiple thread compute, only one thread write to ***one*** faster disk. – UmNyobe Aug 18 '16 at 10:05
0

QueryPerformanceCounter is a system-wide counter, so your result depends on system load and the particulars of the process scheduling.

You could use QueryProcessCycleTime, which returns

The number of CPU clock cycles used by the threads of the process. This value includes cycles spent in both user mode and kernel mode.

You can get a handle to the current process using GetCurrentProcess.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82