[英]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代码:
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.