0

Very simple C programm:

#include <stdio.h>

int main(int, char**) {
    for (int i = 0; i < 500000; i++){
        printf("Test %d\n", i);
    }
}

If I compile & run this in WSL2 with GCC, this will take 1.187855 seconds.

If I compile & run this in Windows with MinGW64, this will take 84.222000 seconds!?!

I tried it on the 4 terminal/consoles for Windows:

  • Powershell 5
  • Powershell 7
  • Terminal
  • CMD

They do not differ significantly from each other.

I tried other code, that does not print anything but calculates something. This code runs equally fast, therefore we're talking specifically about the printf function.

I wouldn't complain of a factor 3 or 4, but a factor of round about 80?!?

Do I miss something here?

Update

As suggested by cforler: You have to edit the buffer by hand:

#include <stdio.h>

int main(int, char**) {
    char buf[65536];
    setvbuf(stdout, buf, _IOFBF, 65536);
    for (int i = 0; i < 500000; i++){
        printf("Test %d\n", i);
    }
}

This accelerates this program to 5.569000 seconds. Still 5 times slower than WSL2, but good enough for me.

SimpleJack
  • 167
  • 1
  • 8
  • Probably has something to do with buffering and/or flushing. – Fiddling Bits Aug 13 '23 at 19:13
  • What happens if you redirect the output to a file? This should significantly speed up the run time. – cforler Aug 13 '23 at 19:18
  • @cforler Yes this does work, but it does not really solve the problem. This would mean, you have to redirect your stdout every time you're on a Windows machine. I think the problem is the terminal/console. The C program is doing exactly what it is supposed to do. – SimpleJack Aug 13 '23 at 19:27
  • 1
    Can you please add the lines " char buf[65536]; setvbuf(stdout, buf, _IOFBF, BUFSIZE);" in front of the for-loop. This should also effect the running speed. – cforler Aug 13 '23 at 19:32
  • 1
    @adabsurdum As usual: The "why" shouldn't be of any consideration. But for the sake of this conversation: The real application does a FFT, and I printed out the complex numbers. Therefore, if you enter a sample of 500.000 measurements you would get 500.000 printf calls. If you enter a sample of 512 measurements you would only get 512 printf calls. – SimpleJack Aug 13 '23 at 19:33
  • [This answer to a C++ question](https://stackoverflow.com/a/18283757/9952196) might be useful even though it's old. – Shawn Aug 13 '23 at 19:36
  • @cforler OK, that does accelerate things. Now, I'm at 5.569000 seconds. – SimpleJack Aug 13 '23 at 19:37
  • And [this Q&A](https://stackoverflow.com/questions/13970675/massive-fprintf-speed-difference-without-std-c99) is a better fit for a duplicate if the change suggested in both fixes the slowdown. They're both like a decade old so who knows if it's still relevant. – Shawn Aug 13 '23 at 19:40
  • I am using Windows and the 2022 MSVC. This `int main(int, char**)` doesn't even compile. – Weather Vane Aug 13 '23 at 19:41
  • @WeatherVane OP said they're using Mingw64 - that is, gcc. – Shawn Aug 13 '23 at 19:42
  • @Shawn how would those arguments be accessed? – Weather Vane Aug 13 '23 at 19:44
  • @WeatherVane They wouldn't. It's a legal C++ thing to omit a name for unused parameters; GCC probably accepts it as an extension in C too. – Shawn Aug 13 '23 at 19:46
  • 1
    Can any human read 500.000 numbers in 84 seconds? – 0___________ Aug 13 '23 at 20:11
  • Also note that MSVC runs at 14 seconds unbuffered and about 3 seconds with the buffered version. So the biggest part of the 84 seconds might be with MingW and not Windows itself. – BoP Aug 14 '23 at 10:41

1 Answers1

1

As usual: The "why" shouldn't be of any consideration. But for the sake of this conversation: The real application does a FFT, and I printed out the complex numbers. Therefore, if you enter a sample of 500.000 measurements you would get 500.000 printf calls.

Unfortunately, you are not right. I do not know any human who can read 500000 numbers in 85 seconds. Probably it will take several hours. So your problem is different:

  • Q1: How to printf numbers not slowing down the calculations?
  • A1: Read about threads.

  • Q2: Do you need to print all the numbers? Will someone read it?
  • A2: Mose likely not. Simply report progress and print out the results.
0___________
  • 60,014
  • 4
  • 34
  • 74