小编the*_*tar的帖子

为什么Python代码在函数中运行得更快?

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 performance benchmarking profiling cpython

809
推荐指数
3
解决办法
6万
查看次数

忘记在做... while 循环

我有一个烦人的错误,我忘记写do一个do ... while循环。

int main() {
  int status=1;
  /*do*/ {
    status = foo();
  } while (status);
}
Run Code Online (Sandbox Code Playgroud)

为什么这仍然编译和运行?在我看来,编译器应该拒绝这是荒谬的或至少发出警告(-Wall我的编译器选项中有)。我正在使用 C++11。

据我所知,大括号代码运行{ ... },然后程序检查 while 子句中的条件。

c++

59
推荐指数
3
解决办法
5891
查看次数

标签 统计

benchmarking ×1

c++ ×1

cpython ×1

performance ×1

profiling ×1

python ×1