Just wanna estimate the process …

Just wanna estimate the process time of my algorithm homework.


Method 1

#include <sys/time.h>  

struct timeval t_start, t_end;  

gettimeofday(&t_start, NULL);  
double start = t_start.tv_sec + (double)t_start.tv_usec / 1000000;  

// Code to be estimated here  

gettimeofday(&t_end, NULL);  
double end = t_end.tv_sec + (double)t_end.tv_usec / 1000000;  

printf("%f seconds\n", end - start);  

This method came from one of my Data Structure homework in last semester.

Can estimated even if the process time is less than one second.

This is the method which I prefer to use.

But, sys/time.h is a POSIX header, not part of the C/C++ standard library.

So, this method can only be implemented on UNIX system.


Method 2

#include <time.h>  

clock_t start = clock();  

//Code to be estimated here  

clock_t end = clock();  

printf("%f seconds\n", (end - start) / (float)CLOCKS_PER_SEC);  

This method seems be common used.

But, seems it cannot estimate those process time less than one second.

Don't Know why... even if I remove the / (float)CLOCKS_PER_SEC. 

It still comes out zero. O_o"


Share


Donation

如果覺得這篇文章對你有幫助, 除了留言讓我知道外, 或許也可以考慮請我喝杯咖啡, 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。

If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.


Related Posts