In this post I will talk about copying files. I will read one file in chunks
of 1MB and write it to another file.
C++ provides three cross platform APIs for I/O (input/output):
C FILE API (fopen, fread, fwrite)
C++ API (std::ifstream, std::ofstream)
POSIX API (open, read, write)
The POSIX API requires a bit of #ifdef-ing to get it working cross platform,
but it’s not that scary.
Reading and writing 1 MB of data should work more or less as fast for
all APIs, right?
I have run the benchmark on my SSD powered Lenovo Core i7 laptop running
Windows 10 and Kubuntu 15.10, and on a SSD powered Raspberry PI2 running
the latest Raspbian.
The code for the benchmark is below:
I have used Boost 1.60 zip package file (125 MB) as the file to copy around.
My test script looks like this:
For the Linux variant just replace @echo off with /bin/bash, > nul with /dev/null and
the line endings
I have tested Visual C++ 2013 32 and 64 bit, Clang 3.7.1 with Visual C++ 2013 32 and 64 bit,
MinGW 4.9.2 32 bit from Qt 5.6 distribution, MinGW 5.3.0 64 bit from Nuwen, Cygwin GCC 5.3.0 64 bit,
and Cygwin Clang 3.7.1 64 bit.
Visual C++ and Clang compilation line was cl /O2 /EHsc test_io.cpp, for MinGW I had
g++ -O2 test_io.cpp -o test_io -std=c++11, and for Cygwin Clang
clang -O2 test_io.cpp -o test_io -std=c++11 -lstdc++.
I have also disabled the real time protection from Windows Defender.
The results are below:
Visual C++ 2013 32
Visual C++ 2013 64
Visual C++ 2015 32
Visual C++ 2015 64
Clang 3.7.1 32
Clang 3.7.1 64
MinGW 4.9.2 32
MinGW 5.3.0 Nuwen
Cygwin GCC 5.3.0 64
Cygwin Clang 3.7.1 64
Surprisingly only MinGW GCC provides the same performance for all three APIs.
Visual C++ and Clang using Visual C++’s CRT library has a 2.87x, respectively a 4.39x
slower C++ API than C or POSIX API !!!
On Cygwin the C and C++ APIs are slower than the POSIX API.
It is very interesting to know why GCC’s libstdc++ behaves on Cygwin slower than on MinGW!
I have booted my Linux distribution and ran the same test there, results below:
GCC 5.2.1 64
Clang 3.6.2 64
Nothing to see here but consistency!
Thanks to this test I have finally managed set up my Raspberry PI2
I had a bit of fun making the USB SSD hard drive to work with Raspberry PI2, increasing
partition size, and so on.
The results of the test a below:
The C++ API for GCC was the fastest!
Interesting to see that Raspberry PI2 was ~12 times slower than my Core i7 laptop.
The POSIX API provides the best results on all platforms tested!