A problem on using boost timer (different time intervals were showed). Can any one with kindness help me?
I am using boost timer in C++ builder 2009 to test the time used in my program.there are the lines: .................... boost::timer t; double timeresult; ................... t.restart(); ........... //my program main body timeresult=t.elapsed(); ShowMessage( " the time consumed by my program is "+FloatToStr(timeresult)); ......................... the program was compiled and linked and worked well. But I find a problem: every time the message showed is not the same time interval, sometimes it is 0.015 and sometimes it is 0.016 and sometimes it is 0. I do not know how it was happened. Does it happen due to the multi-process and multi-thread mode by windows? I do not know how to solve it. My program is quite strict on the time consumed by the computer. Can any one with kindness help me?
2009/9/24 fmingu
I am using boost timer in C++ builder 2009 to test the time used in my program.there are the lines: .................... boost::timer t; double timeresult; ................... t.restart(); ........... //my program main body timeresult=t.elapsed(); ShowMessage( " the time consumed by my program is "+FloatToStr(timeresult)); ......................... the program was compiled and linked and worked well. But I find a problem: every time the message showed is not the same time interval, sometimes it is 0.015 and sometimes it is 0.016 and sometimes it is 0. I do not know how it was happened. Does it happen due to the multi-process and multi-thread mode by windows? I do not know how to solve it. My program is quite strict on the time consumed by the computer. Can any one with kindness help me?
You didn't specify which operating system you are using, but I if it's not a real time OS, there is no guarantee that the same code will take the same time to execute on each run. For small programs (in terms of run time) the difference can be huge (easily an order of magnitude). Roman Perepelitsa.
Roman Perepelitsa wrote:
You didn't specify which operating system you are using, but I if it's not a real time OS, there is no guarantee that the same code will take the same time to execute on each run. For small programs (in terms of run time) the difference can be huge (easily an order of magnitude).
+ the fact that if no side effect occurs in the code, the compiler may decide to not include the code in the xecutable at all.
fmingu wrote:
I am using boost timer in C++ builder 2009 to test the time used in my program.there are the lines: .................... boost::timer t; double timeresult; ...................
t.restart(); ........... //my program main body timeresult=t.elapsed(); ShowMessage( " the time consumed by my program is "+FloatToStr(timeresult)); ......................... the program was compiled and linked and worked well. But I find a problem: every time the message showed is not the same time interval, sometimes it is 0.015 and sometimes it is 0.016 and sometimes it is 0. I do not know how it was happened.
There's nothing strange with that.
Does it happen due to the multi-process and multi-thread mode by windows?
No, it happens because the actual resolution of the timer under Windows is platform-dependent, and on your platform ~15ms. If the exection takes less time, you'll see 0 elapsed.
I do not know how to solve it. My program is quite strict on the time consumed by the computer.
Run the program's main body many times and divide the total time with the number of executions. HTH / Johan
On Thu, Sep 24, 2009 at 3:01 AM, Johan Nilsson
Does it happen due to the multi-process and multi-thread mode by windows?
No, it happens because the actual resolution of the timer under Windows is platform-dependent, and on your platform ~15ms. If the exection takes less time, you'll see 0 elapsed.
I do not know how to solve it. My program is quite strict on the time consumed by the computer.
Run the program's main body many times and divide the total time with the number of executions.
Yes, the around 15ms resolution of the default Windows timer is too coarse IMHO, given the multi-GHz CPUs of today. If what you time fully happens between two ticks, the timer says zero indeed. http://en.wikipedia.org/wiki/Andrew_Koenig wrote a few years back a nice 6 article series on benchmarking (was it Byte, DrDoobs, CUJ, or OOC, I don't recall), and showed you can spin until the clock ticks (up to 15ms worth..., was around 30,000 iterations at that time, in Java code), do your short op, then spin again until the next clock tick, and by approximating the time a spin iter takes, you can get a fairly accurate measurement. Also talked about taking N samples, throwing out the extremes, and then taking the median I think. Windows does support a high resolution timer, but I could never find a good impl to re-use, and it's not portable of course. I wish Boost provided such a high res timer in a portable manner :) --DD
On Thu, Sep 24, 2009 at 4:08 PM, Dominique Devienne > Windows does support a high resolution timer, but I could never find a good impl
to re-use, and it's not portable of course. I wish Boost provided such a high res timer in a portable manner :) --DD
Not sure if boost/posix_time is portable enough for you, but: #include "boost/date_time/posix_time/posix_time.hpp" void example() { boost::posix_time::ptime begin; boost::posix_time::ptime end; begin = boost::posix_time::microsec_clock::universal_time(); do_something(); end = boost::posix_time::microsec_clock::universal_time(); std::cout << boost::posix_time::to_simple_string(end - begin); } Chris
On Thu, Sep 24, 2009 at 3:20 PM, Chris Uzdavinis
On Thu, Sep 24, 2009 at 4:08 PM, Dominique Devienne > Windows does support a high resolution timer, but I could never find a good impl
to re-use, and it's not portable of course. I wish Boost provided such a high res timer in a portable manner :) --DD
Not sure if boost/posix_time is portable enough for you
Thanks. Sounds exactly like what I wanted. I'll give it a spin :) --DD
On Sep 24, 2009, at 1:20 PM, Chris Uzdavinis wrote:
On Thu, Sep 24, 2009 at 4:08 PM, Dominique Devienne > Windows does support a high resolution timer, but I could never find a good impl
to re-use, and it's not portable of course. I wish Boost provided such a high res timer in a portable manner :) --DD
Not sure if boost/posix_time is portable enough for you, but:
#include "boost/date_time/posix_time/posix_time.hpp"
void example() { boost::posix_time::ptime begin; boost::posix_time::ptime end;
begin = boost::posix_time::microsec_clock::universal_time(); do_something(); end = boost::posix_time::microsec_clock::universal_time();
std::cout << boost::posix_time::to_simple_string(end - begin); }
Sorry, but posix_time on Windows is only as good as the low res timer. There's no help there. - Rush
Dominique Devienne wrote:
On Thu, Sep 24, 2009 at 3:01 AM, Johan Nilsson
[snip]
Run the program's main body many times and divide the total time with the number of executions.
Yes, the around 15ms resolution of the default Windows timer is too coarse IMHO, given the multi-GHz CPUs of today. If what you time fully happens between two ticks, the timer says zero indeed.
http://en.wikipedia.org/wiki/Andrew_Koenig wrote a few years back a nice 6 article series on benchmarking (was it Byte, DrDoobs, CUJ, or OOC, I don't recall), and showed you can spin until the clock ticks (up to 15ms worth..., was around 30,000 iterations at that time, in Java code), do your short op, then spin again until the next clock tick, and by approximating the time a spin iter takes, you can get a fairly accurate measurement. Also talked about taking N samples, throwing out the extremes, and then taking the median I think.
Windows does support a high resolution timer, but I could never find a good impl to re-use, and it's not portable of course. I wish Boost provided such a high res timer in a portable manner :) --DD
[shameless self-plug] http://msdn.microsoft.com/en-us/magazine/cc163996.aspx The implementation has its quirks, but the basic functionality is there (modulo a few bugs). It's not portable though. / Johan
participants (7)
-
Chris Uzdavinis
-
Dominique Devienne
-
fmingu
-
Joel Falcou
-
Johan Nilsson
-
Roman Perepelitsa
-
Rush Manbert