Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main()
- {
- clock_t begin, end;
- while(1)
- {
- begin = clock();
- sleep(1); // delay for 1 sec
- end = clock();
- printf("Execution time = %fn",((float)(end-begin)/CLOCKS_PER_SEC));
- }
- }
- #include <stdio.h>
- #include <time.h>
- int main() {
- time_t begin;
- time(&begin);
- // Somethings
- time_t end;
- time(&end);
- printf("Execution time %fn", difftime(end, begin));
- return (0);
- }
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- int main() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- double begin =
- (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
- sleep(2);
- gettimeofday(&tv, NULL);
- double end =
- (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
- printf("Execution time %fn", end - begin);
- return (0);
- }
- #include <stdio.h>
- #include <sys/time.h>
- #include <unistd.h>
- #define ELPS_TIME(func_name) do {
- struct timeval tval_before, tval_after, tval_result;
- gettimeofday(&tval_before, NULL);
- func_name;
- gettimeofday(&tval_after, NULL);
- timersub(&tval_after, &tval_before, &tval_result);
- printf("Time elapsed: %ld.%06ld secondsn",
- (long int)tval_result.tv_sec,
- (long int)tval_result.tv_usec); } while(0)
- static void test_func1() {
- printf("%s:", __FUNCTION__);
- sleep(1);
- }
- static void test_func2() {
- printf("%s:", __FUNCTION__);
- sleep(2);
- }
- int main() {
- ELPS_TIME(test_func1()); //calling test_func1
- ELPS_TIME(test_func2()); //calling test_func2
- return 0;
- }
- test_func1:Time elapsed: 1.000103 seconds
- test_func2:Time elapsed: 2.000974 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement