Execution time of C program

CLOCKS_PER_SEC is a constant which is declared in <time.h>. To get the CPU time used by a task within a C application, use: clock_t begin = clock(); /* here, do your time-consuming job */ clock_t end = clock(); double time_spent = (double)(end – begin) / CLOCKS_PER_SEC; Note that this returns the time as a floating … Read more

Why is reading lines from stdin much slower in C++ than Python?

tl;dr: Because of different default settings in C++ requiring more system calls. By default, cin is synchronized with stdio, which causes it to avoid any input buffering. If you add this to the top of your main, you should see much better performance: std::ios_base::sync_with_stdio(false); Normally, when an input stream is buffered, instead of reading one … Read more