-4

I used C and C++ to write programs to find the number of daffodils, and found that the speed of the C++ program was only half of that of C(I use bash script to repeat run and measure time.). What causes it?

main.c

#include <stdio.h>
#include <stdbool.h>

bool is(int number)
{
    int s = number;
    int t = 0;
    while(number)
    {
        t += (number % 10) * (number % 10) * (number % 10);
        number /= 10;
    }
    return t == s;
}

int main()
{
    for(int i = 100; i < 1000; i++)
    {
        if(is(i))
        {
            printf("%d\n",i);
        }
    }
    return 0;
}

compile: gcc main.c

main.cpp

#include <iostream>

using namespace std;

bool is(int number)
{
    int s = number;
    int t = 0;
    while(number)
    {
        t += (number % 10) * (number % 10) * (number % 10);
        number /= 10;
    }
    return t == s;
}

int main()
{
    for(int i = 100; i < 1000; i++)
    {
        if(is(i))
        {
            cout << i << endl;
        }
    }
    return 0;
}

compile: g++ main.cpp

bash script(test.sh): for i in {1..1000}; do ./a.out; done

test command: time ./test.h

Solved: With static compilation, the speed is the same. It may be that the performance of libstdc++ is not good enough.

Karmylr
  • 29
  • 3
  • 4
    How do you compile the codes? How do you measure the performance? – Thomas Sablik Jan 21 '23 at 05:26
  • 3
    See https://stackoverflow.com/questions/213907/stdendl-vs-n. – Stephen Newell Jan 21 '23 at 05:28
  • @StephenNewell If the output is going to a terminal, `\n` causes a buffer flush in C, so they should be equivalent. – Barmar Jan 21 '23 at 05:30
  • 1
    @Barmar That's the next question. Where is the output going to? To a pipe, a file, the terminal or somewhere else? – Thomas Sablik Jan 21 '23 at 05:43
  • Don't measure performance without optimization. Try it again with `-O3`. – Thomas Sablik Jan 21 '23 at 06:27
  • Also don't time "debug" builds, you really should only test release builds. And use much longer loops, when doing such short loops you also get "noise" from thread scheduling. Then again stream io is always a bit slower then printf (but safer). So make sure you test your algorithm/loops without output. In the end performance should always be measured from a profiler so you know where things are actually slower. – Pepijn Kramer Jan 21 '23 at 08:09

1 Answers1

0

Your benchmark is far from ideal, and its results are largely meaningless.

  1. It measures unoptimised code. Unoptimised compilation is not designed to produce fast code. Use -O2 or -O3.
  2. It measures code code that runs for too few iterations to rule out random fluctuations and edge effects. Increase iteration count by 5 orders of magnitude. If needed, add an outer loop.
  3. It lumps computation and I/O together, and it isn't clear what dominates what. #2 should remedy this, but to be totally sure, do minimal I/O or no I/O at all in your tests (unless you are benchmarking I/O).
  4. It lumps compiled code and bash code together. #2 should eliminate the need for a bash script too.
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243