0

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

//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

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

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.

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)

10
  • 2
    I would assume the difference in running time is due to the difference between the stdio.h and cstdio header files (and functions implemented therein). Commented Apr 15, 2015 at 6:06
  • 3
    Even though it's a trivial amount of code, and it's mostly I/O anyway, you should still compile with optimisations enabled (e.g. -O3) to get meaningful results. Commented Apr 15, 2015 at 6:07
  • 3
    Repeat your test in alternation a few times and see if the results are consistent of if it was just a cache/timing effect. Commented Apr 15, 2015 at 6:24
  • 4
    How often have you tested this? Commented Apr 15, 2015 at 6:25
  • 5
    -1 for benchmarking without optimizations. That's never a good idea. Also, using lot's of I/O in a benchmark is also not that good. You should generate a random set of numbers first, then time the benchmark code and carefully take care of variables that may be optimized out. Commented Apr 15, 2015 at 6:44

3 Answers 3

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 , ...)

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

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know of any differences in the implementations of scanf and printf between the C and C++ versions?
@TimBiegeleisen as i know they are the same.
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.

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.