为什么相同的代码运行时间不同?

时间:2022-10-06 21:02:11

Making a benchmark, i have tested and figured out interesting results.

制作基准测试,我已经测试并找出了有趣的结果。

//gcc -o code_in_c.exe code_in_c.c
#include <stdio.h>
int main() {
    int a, b, c, d, r;
    while (scanf("%d %d %d %d", &a, &b, &c, &d) != -1){
        r = a + b * c + d;
        printf("%d\n", r);
    }
    return 0;
}

and second file for cpp

和cpp的第二个文件

//g++ -o code_in_cpp.exe code_in_cpp.cpp
#include <cstdio>
int main(){
    int a, b, c, d, r;
    while (scanf("%d %d %d %d", &a, &b, &c, &d) != -1){
        r = a + b * c + d;
        printf("%d\n", r);
    }
    return 0;
}

it's the same code, except for first two lines. the program need to read 4 integers from every line perform arithmetic operations (multiply two integers from middle) and add from margins

除了前两行之外,它是相同的代码。程序需要从每一行读取4个整数执行算术运算(从中间乘以两个整数)并从边距添加

1 2 3 4 -> 1 + (2 * 3) + 4 -> 1 + 6 + 4 -> 11

so, testing this with random numbers on a 150 000 lines give me results for cpp

所以,用150 000行的随机数测试这个给出了cpp的结果

Running "code_in_cpp.exe", press ESC to terminate...
Program successfully terminated
  exit code:     0
  time consumed: 2.37 sec
  time passed:   4.34 sec
  peak memory:   2162688 bytes

and for c.

并为c。

Running "code_in_c.exe", press ESC to terminate...
Program successfully terminated
  exit code:     0
  time consumed: 2.87 sec
  time passed:   4.57 sec
  peak memory:   2162688 bytes

So my question is what depends on running time?

所以我的问题是什么取决于运行时间?

(both was running on same machine)

(两者都在同一台机器上运行)

3 个解决方案

#1


3  

this time is depend on very things like: OS context switch, memory management mechanism in OS , mechanism of runnig process (multi thread, multi core cpu , ...)

这个时间取决于以下内容:OS上下文切换,OS中的内存管理机制,runnig进程机制(多线程,多核cpu,...)

so, if we run one program 2 times, necessarily there is no reason that execution time of them become equal.

所以,如果我们运行一个程序2次,则必然没有理由让它们的执行时间变得相等。

#2


1  

I think it's because the g++ links with the C++ standard libraries where the implementation of some functions may be slightly different from the C standard library implementation.

我认为这是因为g ++链接到C ++标准库,其中某些函数的实现可能与C标准库实现略有不同。

C++ standard library is backwards compatible with the C standard library but this doesn't mean there can't be performance improvements seen.

C ++标准库向后兼容C标准库,但这并不意味着看不到性能改进。

#3


1  

In current OS's, program execution time is mostly a non-deterministic variable, meaning that even if you run the same C code twice, it could run in different time, depending on CPU usage and ocupation by the OS, memory management and current ocupation (does OS's uses cache in one of the executions?), etc... Also, as other users pointed out, it probably also depends on the implementation of standart libraries, since, although very similar, they are different languages.

在当前的操作系统中,程序执行时间主要是一个非确定性变量,这意味着即使您运行相同的C代码两次,它也可以在不同的时间运行,具体取决于CPU的使用情况和操作系统的ocupation,内存管理和当前的ocupation(操作系统是否在其中一个执行中使用缓存?)等...另外,正如其他用户指出的那样,它可能还取决于标准库的实现,因为虽然非常相似,但它们是不同的语言。

#1


3  

this time is depend on very things like: OS context switch, memory management mechanism in OS , mechanism of runnig process (multi thread, multi core cpu , ...)

这个时间取决于以下内容:OS上下文切换,OS中的内存管理机制,runnig进程机制(多线程,多核cpu,...)

so, if we run one program 2 times, necessarily there is no reason that execution time of them become equal.

所以,如果我们运行一个程序2次,则必然没有理由让它们的执行时间变得相等。

#2


1  

I think it's because the g++ links with the C++ standard libraries where the implementation of some functions may be slightly different from the C standard library implementation.

我认为这是因为g ++链接到C ++标准库,其中某些函数的实现可能与C标准库实现略有不同。

C++ standard library is backwards compatible with the C standard library but this doesn't mean there can't be performance improvements seen.

C ++标准库向后兼容C标准库,但这并不意味着看不到性能改进。

#3


1  

In current OS's, program execution time is mostly a non-deterministic variable, meaning that even if you run the same C code twice, it could run in different time, depending on CPU usage and ocupation by the OS, memory management and current ocupation (does OS's uses cache in one of the executions?), etc... Also, as other users pointed out, it probably also depends on the implementation of standart libraries, since, although very similar, they are different languages.

在当前的操作系统中,程序执行时间主要是一个非确定性变量,这意味着即使您运行相同的C代码两次,它也可以在不同的时间运行,具体取决于CPU的使用情况和操作系统的ocupation,内存管理和当前的ocupation(操作系统是否在其中一个执行中使用缓存?)等...另外,正如其他用户指出的那样,它可能还取决于标准库的实现,因为虽然非常相似,但它们是不同的语言。