Comparison of Clang 3.2/3.0 and g++ 4.7.3/4.6.3

Comparison is extremely informal, using non-vectorized code that does a lot of multiply/divide/exponent/add. Just trying to get a handle on option performance–Phoronix this isn’t!

End result: using -march=corei7-avx -mllvm -vectorize-loops on clang++ 3.2 Ubuntu 13.04 was the fastest by about 25% advantage over g++ 4.6/clang++ 3.0versions on Ubuntu 12.04, same CPU type

Both PCs used Core i7 Sandy Bridge.
PC1: Ubuntu 12.04, Clang 3.0, g++ 4.6.3
compiled using: clang++ -O3 -std=c++0x foo.cpp -lncurses -lm
normalized time to run compiled code: 1.0 (using this as PC1 reference)

compiled using g++ -O3 -std=c++0x foo.cpp -lncurses -lm
normalized time to run compiled code: 1.0 approximately (not a noticable change)

compiled using g++ -O3 -march=corei7-avx -mtune=corei7-avx -std=c++0x foo.cpp -lncurses -lm
normalized time to run compiled code: 0.95 approximately (slightly better)

PC2: Ubuntu 13.04, Clang 3.2, g++ 4.7.3
(first running the executable copied from PC1) — use as PC2 reference: 1.0

compiled using: clang++ -O3 -std=c++11 foo.cpp -lncurses -lm
normalized time to run compiled code: 0.95 (slightly faster)

compiled using: g++ -O3 -std=c++11 foo.cpp -lncurses -lm
normalized time to run compiled code: 0.95 (slightly faster)

compiled using g++ -O3 -march=corei7-avx -mtune=corei7-avx -std=c++11 foo.cpp -lncurses -lm
normalized time to run compiled code: 0.9 (faster than same options used on Ubuntu 12.04 PC)

compiled using clang++ -O3 -march=corei7-avx -mtune=corei7-avx -mllvm -vectorize-loops -std=c++11 foo.cpp -lncurses -lm
normalized time to run compiled code: 0.8 (a lot faster than Clang 3.0 (no loop vectorization))

C++: writing code to work in both Visual Studio (windows) and g++ (linux)

This is written for someone looking to quickly write some code in C/C++ for mathematical computations.

You might be used to using Visual Studio in Windows instead of the Code::Blocks or Eclipse-CDT available on Windows/Mac/Linux.

Here are a few things to consider to compile your Visual Studio-originated C++ under multi-platform (Windows Mac Linux)

* Command line compilation
I start out by compiling with the command line so that I feel I know what options are being used to compile–so that if I send my code to a friend, they can compile it too.

In Linux for C++ you would compile with EITHER the LLVM-Clang or G++ toolkit (your choice–LLVM-Clang is more modern and is what I use)

Example LLVM-Clang compile command:
clang++ -o foo -std=c++11 foo.cpp -lncurses -lm

Example G++ compile command:

g++ -o foo -std=c++11 foo.cpp -lncurses -lm

Then type <code>./foo</code> to run your program.

* Environment conditionals
Here we modify the “include” part of our code to be appropriate for the operating system we’re compiling for. Let’s assume the original Windows Visual Studio code had
#include <conio.h>

In Linux, we use ncurses instead. So modify to:


#ifdef _WIN32
#include "stdafx.h"
#include <conio.h>
#else // LINUX MAC
#include <ncurses.h>
#endif


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <math.h>

note the “#include “stdafx.h”” is a file used by Visual Studio to set compilation preferences.

remember the “-lncurses” and “-lm” we typed to compile? This tells the compiler to link to math.h and ncurses.h (I assumed you would be using math functions and outputing text to the console).

* writing files

Since we included “fstream” we can write files to disk.  But Windows and Mac/Linux use different directory slashes.  Windows uses backslashes \   and Mac/Linux uses forward slashes /