如何测量for循环的执行时间? [英] How can I measure the execution time of a for loop?

查看:1498
本文介绍了如何测量for循环的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量 for循环在各种平台上的执行时间,例如php,c,python,Java,javascript ...如何衡量?

I want to measure the execution time of for loops on various platforms like php, c, python, Java, javascript... How can i measure it?

我知道这些平台,所以我在谈论这些:

I know these platforms so i am talking about these:

 for (i = 0; i < 1000000; i++)
 {

 }

小修改:

@all某些朋友我们都说编译器会优化这个代码,使这个循环一个无用的循环。我同意这一点。我们可以添加一个小语句,像一些增量语句,但事实是,我只是想计算每次迭代的执行时间在各种语言的循环。通过添加一个增量语句将会增加执行时间并影响结果,因为在各种平台上,增量值的执行时间也不同,这将导致结果无用。
总之,更好的方式,我应该问:

@all Some of the friends of mine are saying compiler will optimize this code making this loop a useless loop. I agree with this. we can add a small statement like some incremental statement, but the fact is I just want to calculate the execution time of per iteration in a loop in various languages. By adding a incremental statement will add up the execution time and that will effect the results, cause on various platforms, execution time for incrementing a value also differ and that will make a result useless. In short, in better way I should ask:

我想计算在各种平台上循环的执行时间。如何做到这一点

编辑---

Python Profilers
性能分析器模块...评估cpu时间...绝对时间..任何建议同时我正在这个...

I came to know about Python Profilers Profiler modules ...which evaluate cpu time... absolute time.. Any suggestions???Meanwhile i am working on this...

推荐答案

注意,这也取决于什么你想实现:你关心你的程序等待的时间,因为它被系统调度程序抢占?以上所有的解决方案都考虑了实际的时间,但这也涉及到其他进程运行而不是自己的时间。

Note that it also depends on what exactly you want to achieve: do you care about the time your program waits due to it being preempted by the system scheduler? All the solutions above take actual time elapsed into consideration, but that also involves the time when other processes run instead of your own.

如果你不在乎,所有的解决方案都是好的。如果你小心,你可能需要一些分析软件来实际看到循环需要多长时间。

If you don't care about this, all of the solutions above are good. If you do care, you probably need some profiling software to actually see how long the loop takes.

我从一个程序开始,只做你的循环和(在linux环境中)至少 time you-prg-executable

I'd start with a program that does nothing but your loop and (in a linux environment at least) do time you-prg-executable.

然后我会调查是否有工具像 time 。不确定,但我会看看JRat for java,gcc的gcov for C和C ++。毫无疑问,有其他语言的类似工具。但是,当然,你需要看看他们是否给出实际的时间。

Then I'd investigate if there are tools which work like time. Not sure, but I'd look at JRat for java, and gcc's gcov for C and C++. No doubt there are similar tools for the other languages. But, of course, you need to see if they give actual time or not.

这篇关于如何测量for循环的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆