2

I am new to programming. I have written a small program in C++ and want to calculate the running time of the program in seconds so that I can plot a graph for input size versus running time. But how will I get the exact running time? When I run the program using Code Blocks, does the result of execution time is the running time of the program or is there any other way to get it?

  • In Unix-land you can just use the `time` command, if the processing time of the program overwhelms its startup and termination time. Otherwise the `` header provides clocks you can use to build a timer used within the code. I.e., *instrumenting* the code. The simplest timer is to use old `clock`. But it has low resolution in Windows. – Cheers and hth. - Alf Sep 08 '16 at 15:29
  • Can you include `time.h` in your program? – Christian Dean Sep 08 '16 at 15:30
  • Yes, it's the running time. But you may not want to use it since it contains start up and other times. – apple apple Sep 08 '16 at 15:30
  • then how will I get the exact running time? @apple apple –  Sep 08 '16 at 15:32
  • This may be what you looking for: http://www.cplusplus.com/forum/beginner/14666/ – Christian Dean Sep 08 '16 at 15:32
  • 1
    since you use c++, I would recommend use instead of (almost the same) – apple apple Sep 08 '16 at 15:34
  • 1
    or if you have c++11 and knows template, maybe try – apple apple Sep 08 '16 at 15:35
  • @appleapple why do you say that, is specify made for c++? – Christian Dean Sep 08 '16 at 15:35
  • @appleapple: `` yields less portable code than ``. It has no advantage that I know. In the first standard, C++98, these headers were invented in order to place identifiers in namespace `std` only, avoiding pollution of the global namespace, but no compiler vendors adhered to that spec. It was changed in C++11. So the original purpose is gone, and there was nothing more. – Cheers and hth. - Alf Sep 08 '16 at 15:35
  • 2
    @Cheersandhth.-Alf How is it less portable? – NathanOliver Sep 08 '16 at 15:35
  • @NathanOliver: Usually people use an unqualified identifier such as `clock`. That's what one's used to from C code. Compile with some other compiler and that identifier is not available in the global namespace. – Cheers and hth. - Alf Sep 08 '16 at 15:37
  • 2
    That is not less portable. If you are using then you need to use `std::`. Using the header wrong is not a lack of portability, – NathanOliver Sep 08 '16 at 15:38
  • @NathanOliver: A header that makes the code less likely to be portable, allowing mistakes to be not caught with some compilers, yields, well you know, code that is less portable. One may deny that that's lack of portability, of course. – Cheers and hth. - Alf Sep 08 '16 at 15:40
  • Well, one may also find std::clock cannot us in some compiler when they include – apple apple Sep 08 '16 at 15:42
  • I do not buy that. If the header puts everything in the standard namespace (which is guaranteed) then you should use it as such. – NathanOliver Sep 08 '16 at 15:42
  • @NathanOliver do you reply to my comment or Cheers? – apple apple Sep 08 '16 at 15:44
  • Well, the facts are that this happens. So, essentially, your authority source, whatever or whoever that is, is wrong. It's just dumb to invite problems. – Cheers and hth. - Alf Sep 08 '16 at 15:44
  • @appleapple No it was to the one above. Your got in right when I pressed enter. – NathanOliver Sep 08 '16 at 15:44
  • The `` headers are more portable because they are the only ones that guarantee `std::` prefix. The `.h` headers may or may not making them less portable for `C++` code and more likely to clash with other libraries in a way that can't easily be disambiguated. – Galik Sep 08 '16 at 15:46
  • What problem are you inviting @Cheersandhth.-Alf If you use `` and `std::name_from_header` there is no problem and you have portable code. Not only is there no problem but now the names are scoped so it can cut down on name conflicts. – NathanOliver Sep 08 '16 at 15:47
  • If you are after the performance of particular piece of code (for performance tuning), you should rather look for some "profiler" solution, timing each interesting code block by hand is a bit tedious, and you may miss some bottleneck in a part of code, which you don't want to measure, as it looks uninteresting. – Ped7g Sep 08 '16 at 15:48
  • @NathanOliver: If you do everything just right there is no problem, agreed. There is no need for type checking either. Just do things right. – Cheers and hth. - Alf Sep 08 '16 at 16:16
  • You can refer this link : http://stackoverflow.com/a/40380118/6180077 . program shows how to calculate time difference. – Abdullah Farweez Mar 27 '17 at 07:29

1 Answers1

0

Use of Boost Cpu Timer, note auto_cpu_timer's destructor prints result.

#include <boost/timer/timer.hpp>

int main(int argc, char** argv)
{
   boost::timer::auto_cpu_timer t;
  // you're stuff here

return 0;
};
Roby
  • 2,011
  • 4
  • 28
  • 55