简体   繁体   English

为什么我的 C 代码和 Python 代码在 for 循环中的迭代需要更多时间

[英]Why does my C code and Python code take more time just for itration in for loop

I have a Python code and a C code.我有一个 Python 代码和一个 C 代码。

My Python code is this:我的 Python 代码是这样的:

import os
import time
count = 0
number_of_itration =100000000
begin = time.time()
for i in range(number_of_itration):
    count += 1
end = time.time()
print(f'Sum is {count}')
print(f"Total runtime of the program is {end - begin} Seconds")

This code takes 12 seconds to execute.此代码需要 12 秒才能执行。

I have a similar C Code:我有一个类似的 C 代码:

int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    start_t = clock();
    for(i=0; i< 100000000; i++) 
    {
        count= count+1;
    }
    end_t = clock();
    total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
    printf("Total time taken by CPU: %f\n", total_t  );
    RunUserInterface ();
    
    return 0;
}

This takes 0.395 seconds.这需要 0.395 秒。

And I have a labview code:我有一个labview代码:

Labview 代码

Which takes just 0.093 seconds.仅需 0.093 秒。

Where am I going wrong?我哪里错了? I was expecting C to run faster.我期待 C 运行得更快。

My C IDE is Lab windows CVI, which is from National Instruments.我的 C IDE 是来自 National Instruments 的 Lab windows CVI。

My system configuration is:我的系统配置是:

系统配置

How can I optimize C code for processor?如何优化处理器的 C 代码?

I could optimize my C code and i found that the C code could execute the same in 50 Milli seconds in release mode我可以优化我的 C 代码,我发现 C 代码可以在发布模式下在 50 毫秒内执行相同的操作

where lab view takes 93 milliseconds实验室视图需要 93 毫秒

but in Python i could int fine anything similar to release more or anything但是在 Python 中,我可以理解任何类似于释放更多或任何东西的东西

If you have debugging turned off, LabVIEW will constant fold that For Loop during compilation.如果您关闭了调试,LabVIEW 将在编译期间不断折叠该 For 循环。 It won't take any time at all to execute because it'll all reduce to a single pre-computed constant value.它根本不需要任何时间来执行,因为它会全部减少到一个预先计算的常量值。 Change the input to the For Loop's shift register from a constant to a control and add the control to the connector pane.将 For 循环移位寄存器的输入从常量更改为控件,并将控件添加到连线板。 You should see the time increase to be about equal to the C code.您应该看到时间增加大约等于 C 代码。 I suspect if you change your compile options on the C code, you'd see the time there decrease.我怀疑如果您更改 C 代码上的编译选项,您会看到那里的时间减少了。

Python doesn't do those kinds of optimizations. Python 不做这些优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:为什么这段代码会永远存在(无限循环?) - Python: why does this code take forever (infinite loop?) 为什么这段代码要花这么长时间? - Why does this code take such a long time? 简单的循环迭代字典python - Simple for loop itration dictionary python 为什么我的多处理代码需要这么长时间才能在 python 3.9 中完成,而不是 python 2.7。 代码改进? - Why does my multiprocessing code take so long to complete in python 3.9, but not python 2.7. Code improvements? 为什么我的Python代码跳过while循环? - Why does my Python code skips a while loop? Python 为什么我的代码会导致无限循环? - Python Why does my code result in an infinite loop? 为什么一个包含100亿次迭代的大型for循环在Python中运行的时间要比在C中运行的时间长? - Why does a large for loop with 10 billion iterations take a much longer time to run in Python than in C? 为什么我的vscode要求我写“python3”而不是“python”来运行一行代码 - Why does my vscode require me to write "python3" instead of just "python" to run a line of code 为什么此for循环在Python中占用太多时间和空间 - Why does this for-loop take too much time and space in Python 为什么即使是很小的输入也要花我永远的时间来运行我的python代码? - Why does it take forever to run my python code even for a small input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM