Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the execution time of the same code on the same computer could be different?

Tags:

c

I am new to C programming, I wrote code and want to get it's runtime. Here is what I did it. Every time I run my code, I get a different run time value. Is it right? or is something wrong with my code?

int main(int argc, char *argv[])
{
    time_t start,end;
    start=clock();
    // this part is some operation

    end=clock();
    int running_time=end-start;
    printf("Time taken: %d",running_time);
    return 0;
}
like image 475
SHANG ZHOU Avatar asked Jan 26 '23 20:01

SHANG ZHOU


1 Answers

Nothing wrong with code. Its just understanding of OS scheduler for your program execution, It varies all time.

like image 118
Muhammad Azam Avatar answered Feb 01 '23 22:02

Muhammad Azam