0

私は自分のコードでこれに気づいていたので、Boost.timer に付属のサンプル コードを使用して検証することにしました。tdm64 コンパイラでは動作しないのでしょうか?

結果は次のとおりです。

% g++ --version
g++.exe (tdm64-1) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

% 
% g++ -Wall -ggdb3 -o auto_cpu_timer_example boost_1_49_0/libs/timer/example/auto_cpu_timer_example.cpp -lboost_timer -lboost_chrono -lboost_system
% 
% g++ -Wall -ggdb3 -o timex boost_1_49_0/libs/timer/example/timex.cpp  -lboost_timer -lboost_chrono -lboost_system
%
% auto_cpu_timer_example.exe 
 0.000000s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (0.0%)
%
%
% ./timex -v auto_cpu_timer_example.exe 
 0.000000s wall, 0.000000s user + 0.000000s system = 0.000000s CPU (0.0%)
command: "auto_cpu_timer_example.exe"
 0.000000s elapsed wall-clock time
% 
4

1 に答える 1

0

The example code is broken:

#include <boost/timer/timer.hpp>
#include <cmath>

int main()
{
  boost::timer::auto_cpu_timer t;

  for (long i = 0; i < 100000000; ++i)
    std::sqrt(123.456L); // burn some time

  return 0;
}

The std::sqrt function is a pure function. That is, it has no effects other than to return a value based solely on its parameter. If that value isn't used, the function invocation can be discarded. Since the return value isn't used, the function can be skipped. This results in the code using no time at all.

You can fix it this way:

  long t = 0;
  for (long i = 0; i < 100000000; ++i)
    t += std::sqrt(123.456L + i); // burn some time
  std::cout << "t = " << t << std::endl;

Now, the return value is used and the parameter is different every time. This means the function will be invoked repeatedly, giving you something to time.

于 2012-08-09T01:01:44.463 に答える