Calculating time of execution with time() function

前端 未结 5 1424
盖世英雄少女心
盖世英雄少女心 2020-12-07 06:12

I was given the following HomeWork assignment,

Write a program to test on your computer how long it takes to do nlogn, n2, n5, 2n, and n! additions

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 06:45

    Your code is executed too fast to be detected by time function returning the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC.

    Try to use this piece of code:

    inline long getCurrentTime() {
        timeb timebstr;
        ftime( &timebstr );
        return (long)(timebstr.time)*1000 + timebstr.millitm;
    }
    

    To use it you have to include sys/timeb.h.

    Actually the better practice is to repeat your calculations in the loop to get more precise results.

提交回复
热议问题