4 ms·
I don’t like the article. The code is not portable to other compilers/OSes so I can’t even run it here on Windows. The code uses inline assembly a lot, i.e. op
by Const-me 8y ago
I don’t like the article.
The code is not portable to other compilers/OSes so I can’t even run it here on Windows. The code uses inline assembly a lot, i.e. optimizer likely failed to achieve much, and for code like this optimizations are very important.
The reason for inline assembly is also worse mentioning, it’s for rdtsc instruction. Mainstream CPUs have dynamic frequency for the last decade. Recent CPUs return rdtsc values unrelated to the count of instruction it executed, it’s a counter running at some constant frequency. The code uses AVX, the CPU is therefore at least sandy bridge or AMD equivalent. Now in 2018, __asm rdtsc is no longer a good method for performance measures.
- londons_explore 8y agoYou don't need to use rtdsc for this... You can use any library/os specific timing functions. Timing accuracy doesn't even really matter because you can just do more iterations. Since it's outside the main loop, it shouldn't affect compiler optimizations.
- justrobert 8y agoI'd say if you're interested in performance to this level, it is useful to know both at&t inline asm syntax on top of the simpler microsoft inline asm syntax.
- ordu 8y ago> The code uses inline assembly a lot, i.e. optimizer likely failed to achieve much, and for code like this optimizations are very important. Yes, optimizations are important, but not for the question the autor tries to ask. To answer his question he need to control optimizations, to have vectorized code and not vectorized one. The simpliest way to control optimizations is to turn them off and to do them by hand. > Recent CPUs return rdtsc values unrelated to the count of instruction it executed, it’s a counter running at some constant frequency. So this counter is the best fit to measure speed of code. What the point to count number of instructions executed when different instructions take different time to execute? But if rdtsc counts clock ticks, than it fits best. It is OS independant way to measure time. It is compiler dependant way, but it works with gcc and clang, the compilers you can get for free. Alternatively it is not so hard to rewrite two non-empty __asm statements into any asm syntax your compiler understands. There are total of 6 instructions.
- justrobert 8y agoOften you can turn off frequency scaling so you can have rdtsc still be useful.
- Const-me 8y ago> it works with gcc and clang, the compilers you can get for free When I’m building for Windows, I usually use Visual C++ 2017 community edition, the compiler I got for free. > into any asm syntax your compiler understands When building 64 bit binaries, my compiler doesn’t understand any syntax. The portable way to produce that instruction is __rdtsc() intrinsic, it’s supported in all major compilers just in different headers, x86intrin.h for gcc/clang, intrin.h for msvc. > There are total of 6 instructions. Right, but I’m not sure assembly, inline or not, has good compatibility with advanced optimization techniques implemented by all modern compilers. It’s just too low level: harder to inline, impossible to move stuff across registers…
- ordu 8y ago> When I’m building for Windows, I usually use Visual C++ 2017 community edition, the compiler I got for free. Is it an obstacle to get one more free compiler? > When building 64 bit binaries, my compiler doesn’t understand any syntax. Is it really? I'm surprised. > The portable way to produce that instruction is __rdtsc() intrinsic, it’s supported in all major compilers just in different headers, x86intrin.h for gcc/clang, intrin.h for msvc. I like to learn it. Thank you. > Right, but I’m not sure assembly, inline or not, has good compatibility with advanced optimization techniques implemented by all modern compilers. It’s just too low level: harder to inline, impossible to move stuff across registers… Its not relevant: this code do not rely on compiler optimizations.
- Const-me 8y ago> Is it an obstacle to get one more free compiler? I have both gcc and clang installed but they’re for Linux. I’m using Windows as my main OS, and prefer running native binaries, e.g. because tools for debugging & profiling. > Its not relevant: this code do not rely on compiler optimizations. Right, and that’s GCC’s problems — MSVC-produced code is optimized, and is free from the described issue. You know how I found that? I’ve made a version portable across compilers, built with VC++ and run on Windows: https://github.com/Const-me/MemoryAccessCosts https://github.com/Const-me/MemoryAccessCosts I don’t have Intel compiler but my expectation is it’ll work even better.