Saturday, January 15, 2022

[otodgekv] printf vs cout speed

we demonstrate printf performing over 10x faster than C++ iostreams cout.  this test program prints a bunch of intergers:

#include <cstdlib>
#include <cstdio>

#include <iostream>
using std::endl;
using std::cerr;

void testc(int n){
  for(int i=0;i<n;++i){
    printf("%d\n",i);
  }
  printf("completed printf\n");
}

void testcpp(int n){
  for(int i=0;i<n;++i){
    std::cout << i << endl;
  }
  std::cout << "completed cout" << endl;
}

int main(int argc,char**argv){
  if(argc<3){
    cerr << "args: n choice" << endl;
    return 1;
  }
  // 50 million is a good n
  int n=atoi(argv[1]);

  int choice=atoi(argv[2]);
  if(0==choice)
    testc(n);
  else if(1==choice)
    testcpp(n);
  else {
    cerr << "bad choice" << endl;
  }
}

$ g++ -v
...
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

Test printf:

$ command time ./a.out 50000000 0 | tail -2
4.04user 0.67system 0:04.71elapsed 99%CPU (0avgtext+0avgdata 3488maxresident)k
0inputs+0outputs (0major+125minor)pagefaults 0swaps
49999999
completed printf

$ command time ./a.out 50000000 0 > /dev/null
4.04user 0.05system 0:04.10elapsed 100%CPU (0avgtext+0avgdata 3456maxresident)k
0inputs+0outputs (0major+124minor)pagefaults 0swaps

Test iostreams cout:

$ command time ./a.out 50000000 1 | tail -2
25.52user 41.49system 1:07.03elapsed 99%CPU (0avgtext+0avgdata 3392maxresident)k
0inputs+0outputs (0major+123minor)pagefaults 0swaps
49999999
completed cout

$ command time ./a.out 50000000 1 > /dev/null
23.86user 22.85system 0:46.71elapsed 100%CPU (0avgtext+0avgdata 3348maxresident)k
0inputs+8outputs (0major+124minor)pagefaults 0swaps

when piping to tail -2, elapsed time slowdown factor was 14.2 .  when redirecting to /dev/null , elapsed time slowdown factor was 11.4 .

motivation for printing a bunch of integers was emitting Netpbm's easy-to-generate P3 text-only "noraw" PPM image format, then piping to pnmtopng .

on a different machine and OS (Debian Buster) and gcc version 8.3.0 (Debian 8.3.0-6), still saw significant but not quite as dramatic slowdowns.  when piping to tail -2, elapsed time slowdown factor was 9.9 .  when redirecting to /dev/null , elapsed time slowdown factor was 6.4 .

with gcc version 10.2.1 20210110 (Debian 10.2.1-6), Debian Bullseye, on Xen Dom0 under constant high load: slowdown factors 48.4 and 19.2 .

the classic "Hello, world!" task is rather nontrivial.

No comments :