Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clock_t begin, end;
- double time_spent;
- begin = clock();
- /* here, do your time-consuming job */
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- $ time ./a.out
- #include <time.h>
- int main()
- {
- clock_t tic = clock();
- my_expensive_function_which_can_spawn_threads();
- clock_t toc = clock();
- printf("Elapsed: %f secondsn", (double)(toc - tic) / CLOCKS_PER_SEC);
- return 0;
- }
- #include <sys/time.h>
- struct timeval tv1, tv2;
- gettimeofday(&tv1, NULL);
- /* stuff to do! */
- gettimeofday(&tv2, NULL);
- printf ("Total time = %f secondsn",
- (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
- (double) (tv2.tv_sec - tv1.tv_sec));
- /* ISO/IEC 9899:1990 7.12.1: <time.h>
- The macro `CLOCKS_PER_SEC' is the number per second of the value
- returned by the `clock' function. */
- /* CAE XSH, Issue 4, Version 2: <time.h>
- The value of CLOCKS_PER_SEC is required to be 1 million on all
- XSI-conformant systems. */
- # define CLOCKS_PER_SEC 1000000l
- # if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
- /* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK
- presents the real value for clock ticks per second for the system. */
- # include <bits/types.h>
- extern long int __sysconf (int);
- # define CLK_TCK ((__clock_t) __sysconf (2)) /* 2 is _SC_CLK_TCK */
- # endif
- #include <time.h>
- #define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec +
- ruse.ru_stime.tv_sec + 1e-6 *
- (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec))
- int main(void) {
- time_t start, end;
- double first, second;
- // Save user and CPU start time
- time(&start);
- first = CPU_TIME;
- // Perform operations
- ...
- // Save end time
- time(&end);
- second = CPU_TIME;
- printf("cpu : %.2f secsn", second - first);
- printf("user : %d secsn", (int)(end - start));
- }
- #include <time.h>
- #include <stdio.h>
- int main(){
- clock_t start = clock();
- // Execuatable code
- clock_t stop = clock();
- double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
- printf("Time elapsed in ms: %f", elapsed);
- }
- double difftime(time_t time1, time_t time0);
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement