Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.os.linux.misc > #77482

Re: Simple Programming Challenge

From Ralf Fassel <ralfixx@gmx.de>
Newsgroups comp.os.linux.misc
Subject Re: Simple Programming Challenge
Date 2025-11-13 16:16 +0100
Message-ID <ygawm3uote3.fsf@akutech.de> (permalink)
References <18774b74533f13ac$14097$3668313$802601b3@news.usenetexpress.com> <5L1RQ.59910$3VN7.27458@fx41.iad> <project-20251112171620@ram.dialup.fu-berlin.de> <18774fee1aa33cb0$22907$3467166$802601b3@news.usenetexpress.com> <benchmark-20251113142130@ram.dialup.fu-berlin.de>

Show all headers | View raw


* ram@zedat.fu-berlin.de (Stefan Ram)
|   I know that terminal output is slow, so I just wrote to memory
|   and have one single terminal output at the end. However, for my
|   benchmark I used ">/dev/nul" and found out that:
>
|   - My program is much slower than the C programs shown here.
>
|   - The most dramatic speed-up resulted from using C instead of C++.
|     (So the "++" in "C++" actually constitute the "too many additions".)

I'd guess this is mostly due to the overhead of loading (and
initializing) additional shared libraries.  If you take the identical C
program and compile it with g++ instead of gcc, runtime increases:

    % gcc -O -o t5 t5.c
    % /usr/bin/time -f "%U" bash -c 'for i in {1..1000}; do ./t5 > /dev/null 2>&1; done'
    0.58
    % ldd t5
            linux-vdso.so.1 (0x00007ffe63df9000)
            libc.so.6 => /lib64/libc.so.6 (0x00007fb8bf800000)
            /lib64/ld-linux-x86-64.so.2 (0x00007fb8bfbc4000)
            

    % g++ -O -o t5 t5.c
    % /usr/bin/time -f "%U" bash -c 'for i in {1..1000}; do ./t5 > /dev/null 2>&1; done'
    1.02
    % ldd t5
            linux-vdso.so.1 (0x00007ffec1997000)
            libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fbf58200000)
            libm.so.6 => /lib64/libm.so.6 (0x00007fbf58529000)
            libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fbf584fc000)
            libc.so.6 => /lib64/libc.so.6 (0x00007fbf57e00000)
            /lib64/ld-linux-x86-64.so.2 (0x00007fbf58638000)

|   - But even after I ported my program to C and improved it a bit, the 
|     programs that just call "printf" multiple times are still faster.

Using stdio usually is also printing to memory and only doing actual I/O
when the output buffer needs flushing (in this case probably only at
exit, since the output is 'only' 4674 bytes).

Only when you use write(2) directly, runtime increases, most probably
due to context switches from user to kernel and back, since no actual
I/O involving disks or terminal is done.

    # t6 using sprintf(3) for the numbers and write(2) for all text
    /usr/bin/time -f "%U" bash -c 'for i in {1..1000}; do ./t6 > /dev/null 2>&1; done'
    1.31

R'

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Simple Programming Challenge Joe Fantastic <jf@linux.rocks> - 2025-11-12 15:19 +0000
  Re: Simple Programming Challenge "Joel W. Crump" <joelcrump@gmail.com> - 2025-11-12 10:31 -0500
    Re: Simple Programming Challenge John Ames <commodorejohn@gmail.com> - 2025-11-12 08:07 -0800
    Re: Simple Programming Challenge ram@zedat.fu-berlin.de (Stefan Ram) - 2025-11-12 16:18 +0000
      Re: Simple Programming Challenge Joe Fantastic <jf@linux.rocks> - 2025-11-12 16:41 +0000
        Re: Simple Programming Challenge Pancho <Pancho.Jones@protonmail.com> - 2025-11-12 17:29 +0000
          Re: Simple Programming Challenge Pancho <Pancho.Jones@protonmail.com> - 2025-11-12 17:33 +0000
            Re: Simple Programming Challenge Richard Kettlewell <invalid@invalid.invalid> - 2025-11-13 00:00 +0000
              Re: Simple Programming Challenge Pancho <Pancho.Jones@protonmail.com> - 2025-11-13 10:45 +0000
        Re: Simple Programming Challenge Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-11-12 23:30 +0000
        Re: Simple Programming Challenge ram@zedat.fu-berlin.de (Stefan Ram) - 2025-11-13 13:24 +0000
          Re: Simple Programming Challenge Ralf Fassel <ralfixx@gmx.de> - 2025-11-13 16:16 +0100
      Re: Simple Programming Challenge Fritz Wuehler <fritz@spamexpire-202511.rodent.frell.theremailer.net> - 2025-11-12 18:37 +0100
    Re: Simple Programming Challenge Brock McNuggets <brock.mcnuggets@gmail.com> - 2025-11-12 20:05 +0000
      Re: Simple Programming Challenge "Joel W. Crump" <joelcrump@gmail.com> - 2025-11-12 15:30 -0500
        Re: Simple Programming Challenge Brock McNuggets <brock.mcnuggets@gmail.com> - 2025-11-12 20:50 +0000
  Re: Simple Programming Challenge John McCue <jmclnx@gmail.com.invalid> - 2025-11-12 17:22 +0000
    Re: Simple Programming Challenge c186282 <c186282@nnada.net> - 2025-11-12 23:57 -0500
  Re: Simple Programming Challenge Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-11-12 23:24 +0000

csiph-web