Introduction to Algorithm Competition Classics (2) – Program Timer

Words: 211

When you feel the program is computing too slowly
you can use a “timer” to see exactly how slow it is

Specifically, it is a time.h header file
which lets you call the timing function clock()
Divide the obtained value by the constant CLOCK_PER_SEC
to get a value in seconds

How to write the code
First include the header file

cpp
1
#include<time.h>

Then at the end of the program write

cpp
1
printf("程序用时 = %.2f\n",(double)clock()/CLOCKS_PER_SEC);

But in this way you will find that the program time is too large
because the starting point of this timing is when the program is opened
so the time taken for keyboard input will also be included
so you can solve it with the command line

Open CMD (Command Prompt)

Enter
cd file directory
to change the current directory to the specified directory
For example:

Then enter echo input data | program name
For example:

abc is the filename of the program I created

You can get some data about input and output
so you can analyze it

There is still quite a difference from the data given in the book
It can be seen that my little computer’s computing ability is still okay~
Hahaha

Introduction to Algorithm Competition Classic — Liu Jiaru P26

Loading...