我有一个在Linux上运行的C++应用程序,我正在优化它.如何确定代码的哪些区域运行缓慢?
项目欧拉和其他编码竞赛通常有最长的运行时间或人们吹嘘他们的特定解决方案运行的速度.使用python,有时候这些方法有点像kludgey - 即添加时间码__main__.
分析python程序运行多长时间的好方法是什么?
我需要以毫秒为单位获得执行时间.
我最初在2008年问过这个问题.然后,接受的答案是使用新的Date().getTime()但是,我们现在都同意使用标准的performance.now() API更合适.因此,我正在改变对此问题的接受答案.
def main():
for i in xrange(10**8):
pass
main()
Run Code Online (Sandbox Code Playgroud)
Python中的这段代码运行(注意:时序是在Linux中的BASH中使用时间函数完成的.)
real 0m1.841s
user 0m1.828s
sys 0m0.012s
Run Code Online (Sandbox Code Playgroud)
但是,如果for循环没有放在函数中,
for i in xrange(10**8):
pass
Run Code Online (Sandbox Code Playgroud)
然后它会运行更长的时间:
real 0m4.543s
user 0m4.524s
sys 0m0.012s
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
我想知道我的Python应用程序的内存使用情况,并且特别想知道哪些代码块/部分或对象占用了大部分内存.Google搜索显示商业广告是Python Memory Validator(仅限Windows).
我没有尝试任何人,所以我想知道哪一个是最好的考虑:
提供大部分细节.
我必须对代码进行最少或不做任何更改.
您在使用.net程序时使用了哪些分析器,您会特别推荐哪些?
我有一段javascript代码,我正在使用node.js解释器执行.
for(var i = 1; i < LIMIT; i++){
db.users.save({id : i, name : "MongoUser [" + i + "]"}, function(err, saved) {
if( err || !saved ) console.log("Error");
else console.log("Saved");
});
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何测量这些数据库插入操作所花费的时间.我可以计算这段代码之前和之前的Date值的差异,但由于代码的异步性质,这将是不正确的.
分析PHP脚本最简单的方法是什么?
我喜欢在上面添加一些内容,这些内容显示了所有函数调用的转储以及它们花了多长时间,但我也可以将特定函数放在一边.
我尝试过使用microtime函数:
$then = microtime();
myFunc();
$now = microtime();
echo sprintf("Elapsed: %f", $now-$then);
Run Code Online (Sandbox Code Playgroud)
但这有时会给我带来负面结果.另外,在我的代码中撒上这些东西会很麻烦.
测量执行时间的R是否有标准化的方法?
显然我可以system.time在执行之前和之后采取,然后采取其中的差异,但我想知道是否有一些标准化的方式或功能(想要不发明轮子).
我似乎记得我曾经使用过如下的东西:
somesysfunction("myfunction(with,arguments)")
> Start time : 2001-01-01 00:00:00 # output of somesysfunction
> "Result" "of" "myfunction" # output of myfunction
> End time : 2001-01-01 00:00:10 # output of somesysfunction
> Total Execution time : 10 seconds # output of somesysfunction
Run Code Online (Sandbox Code Playgroud) JS代码的Chrome CPU分析中的"自我"和"总"列之间有什么区别?

profiling ×10
javascript ×3
performance ×3
python ×3
.net ×1
benchmarking ×1
c# ×1
c++ ×1
cpython ×1
node.js ×1
php ×1
profiler ×1
r ×1
time ×1
unix ×1