Linux 轻松测量经过的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2808398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easily measure elapsed time
提问by hap497
I am trying to use time()to measure various points of my program.
我正在尝试使用time()来测量我的程序的各个点。
What I don't understand is why the values in the before and after are the same? I understand this is not the best way to profile my program, I just want to see how long something take.
我不明白的是为什么之前和之后的值是一样的?我知道这不是描述我的程序的最佳方式,我只是想看看某事需要多长时间。
printf("**MyProgram::before time= %ld\n", time(NULL));
doSomthing();
doSomthingLong();
printf("**MyProgram::after time= %ld\n", time(NULL));
I have tried:
我试过了:
struct timeval diff, startTV, endTV;
gettimeofday(&startTV, NULL);
doSomething();
doSomethingLong();
gettimeofday(&endTV, NULL);
timersub(&endTV, &startTV, &diff);
printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
How do I read a result of **time taken = 0 26339
? Does that mean 26,339 nanoseconds = 26.3 msec?
我如何读取结果**time taken = 0 26339
?这是否意味着 26,339 纳秒 = 26.3 毫秒?
What about **time taken = 4 45025
, does that mean 4 seconds and 25 msec?
怎么样**time taken = 4 45025
,这是否意味着 4 秒和 25 毫秒?
回答by vodkhang
the time(NULL) function will return the number of seconds elapsed since 01/01/1970 at 00:00. And because, that function is called at different time in your program, it will always be different Time in C++
time(NULL) 函数将返回自 01/01/1970 00:00 以来经过的秒数。因为,该函数在程序中的不同时间被调用,所以在 C++ 中总是不同的 时间
回答by Mike Weller
Internally the function will access the system's clock, which is why it returns different values each time you call it. In general with non-functional languages there can be many side effects and hidden state in functions which you can't see just by looking at the function's name and arguments.
在内部,该函数将访问系统的时钟,这就是为什么每次调用它时它都会返回不同的值。通常,对于非函数式语言,函数中可能存在许多副作用和隐藏状态,您仅通过查看函数的名称和参数是看不到的。
回答by philant
time(NULL)
returns the number of seconds elapsed since 01/01/1970 at 00:00 (the Epoch). So the difference between the two values is the number of seconds your processing took.
time(NULL)
返回自 01/01/1970 00:00(Epoch)以来经过的秒数。因此,这两个值之间的差值是您的处理所用的秒数。
int t0 = time(NULL);
doSomthing();
doSomthingLong();
int t1 = time(NULL);
printf ("time = %d secs\n", t1 - t0);
You can get finer results with getttimeofday()
, which return the current time in seconds, as time()
does and also in microseconds.
您可以使用 获得更好的结果getttimeofday()
,它以秒为单位返回当前时间time()
,也以微秒为单位。
回答by RvdK
Windows only:(The Linux tag was added after I posted this answer)
仅限 Windows:(在我发布此答案后添加了 Linux 标签)
You can use GetTickCount()to get the number of milliseconds that have elapsed since the system was started.
您可以使用GetTickCount()获取自系统启动以来经过的毫秒数。
long int before = GetTickCount();
// Perform time-consuming operation
long int after = GetTickCount();
回答by kibibu
They are they same because your doSomething function happens faster than the granularity of the timer. Try:
它们是相同的,因为您的 doSomething 函数比计时器的粒度发生得更快。尝试:
printf ("**MyProgram::before time= %ld\n", time(NULL));
for(i = 0; i < 1000; ++i) {
doSomthing();
doSomthingLong();
}
printf ("**MyProgram::after time= %ld\n", time(NULL));
回答by wilhelmtell
The time(NULL)
function call will return the number of seconds elapsed since epoc: January 1 1970. Perhaps what you mean to do is take the difference between two timestamps:
该time(NULL)
自EPOC函数调用将返回的秒数经过:1月1日1970年你的意思做的是采取两个时间戳之间的差别也许是什么:
size_t start = time(NULL);
doSomthing();
doSomthingLong();
printf ("**MyProgram::time elapsed= %lds\n", time(NULL) - start);
回答by wilhelmtell
#include <ctime>
void f() {
using namespace std;
clock_t begin = clock();
code_to_time();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}
The time()
function is only accurate to within a second, but there are CLOCKS_PER_SEC
"clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.
该time()
功能只精确到秒之内,但也有CLOCKS_PER_SEC
一秒钟内“时钟”。这是一种简单、便携的测量方法,尽管它过于简化了。
回答by David Bo?jak
The reason both values are the same is because your long proceduredoesn't take that long - less than one second. You can try just adding a long loop (for (int i = 0; i < 100000000; i++) ; ) at the end of the function to make sure this is the issue, then we can go from there...
两个值相同的原因是因为您的漫长过程不会花费那么长时间 - 不到一秒。您可以尝试在函数末尾添加一个长循环(for (int i = 0; i < 100000000; i++) ; )以确保这是问题所在,然后我们可以从那里开始......
In case the above turns out to be true, you will need to find a different system function (I understand you work on linux, so I can't help you with the function name) to measure time more accurately. I am sure there is a function simular to GetTickCount() in linux, you just need to find it.
如果上述情况属实,您将需要找到不同的系统函数(我了解您在 linux 上工作,所以我无法帮助您提供函数名称)来更准确地测量时间。我确信在 linux 中有一个类似于 GetTickCount() 的函数,你只需要找到它。
回答by Didier Trosset
The values printed by your second program are seconds, and microseconds.
第二个程序打印的值是秒和微秒。
0 26339 = 0.026'339 s = 26339 μs
4 45025 = 4.045'025 s = 4045025 μs
回答by AKN
As I can see from your question, it looks like you want to know the elapsed time after execution of some piece of code. I guess you would be comfortable to see the results in second(s). If so, try using difftime()
function as shown below. Hope this solves your problem.
正如我从您的问题中看到的那样,您似乎想知道执行某段代码后经过的时间。我想你会很舒服地在几秒钟内看到结果。如果是这样,请尝试使用difftime()
如下所示的功能。希望这能解决您的问题。
#include <time.h>
#include <stdio.h>
time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif );