compiler-optimization

Inconsistent behavior of compiler optimization of unused string

别说谁变了你拦得住时间么 提交于 2019-12-02 16:28:22
I am curious why the following piece of code: #include <string> int main() { std::string a = "ABCDEFGHIJKLMNO"; } when compiled with -O3 yields the following code: main: # @main xor eax, eax ret (I perfectly understand that there is no need for the unused a so the compiler can entirely omit it from the generated code) However the following program: #include <string> int main() { std::string a = "ABCDEFGHIJKLMNOP"; // <-- !!! One Extra P } yields: main: # @main push rbx sub rsp, 48 lea rbx, [rsp + 32] mov qword ptr [rsp + 16], rbx mov qword ptr [rsp + 8], 16 lea rdi, [rsp + 16] lea rsi, [rsp +

Java program runs slower when code that is never executed is commented out

血红的双手。 提交于 2019-12-02 15:22:46
I observed some strange behaviour in one of my Java programs. I have tried to strip the code down as much as possible while still being able to replicate the behaviour. Code in full below. public class StrangeBehaviour { static boolean recursionFlag = true; public static void main(String[] args) { long startTime = System.nanoTime(); for (int i = 0; i < 10000; i ++) { functionA(6, 0); } long endTime = System.nanoTime(); System.out.format("%.2f seconds elapsed.\n", (endTime - startTime) / 1000.0 / 1000 / 1000); } static boolean functionA(int recursionDepth, int recursionSwitch) { if

Is it possible to implement bitwise operators using integer arithmetic?

淺唱寂寞╮ 提交于 2019-12-02 14:06:30
I am facing a rather peculiar problem. I am working on a compiler for an architecture that doesn't support bitwise operations. However, it handles signed 16-bit integer arithmetics and I was wondering if it would be possible to implement bitwise operations using only: Addition ( c = a + b ) Subtraction ( c = a - b ) Division ( c = a / b ) Multiplication ( c = a * b ) Modulus ( c = a % b ) Minimum ( c = min(a, b) ) Maximum ( c = max(a, b) ) Comparisons ( c = (a < b), c = (a == b), c = (a <= b), et.c. ) Jumps ( goto, for, et.c. ) The bitwise operations I want to be able to support are: Or ( c =

Float compile-time calculation not happening?

五迷三道 提交于 2019-12-02 10:46:42
问题 A little test program: #include <iostream> const float TEST_FLOAT = 1/60; const float TEST_A = 1; const float TEST_B = 60; const float TEST_C = TEST_A / TEST_B; int main() { std::cout << TEST_FLOAT << std::endl; std::cout << TEST_C << std::endl; std::cin.ignore(); return 0; } Result : 0 0.0166667 Tested on Visual Studio 2008 & 2010. I worked on other compilers that, if I remember well, made the first result like the second result. Now my memory could be wrong, but shouldn't TEST_FLOAT have

libsvm compiled with AVX vs no AVX

那年仲夏 提交于 2019-12-02 05:14:56
I compiled a libsvm benchmarking app which does svm_predict() 100 times on the same image using the same model. The libsvm is compiled statically (MSVC 2017) by directly including svm.cpp and svm.h in my project. EDIT: adding benchmark details for (int i = 0; i < counter; i++) { std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); double label = svm_predict(model, input); std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();

Float compile-time calculation not happening?

淺唱寂寞╮ 提交于 2019-12-02 04:23:38
A little test program: #include <iostream> const float TEST_FLOAT = 1/60; const float TEST_A = 1; const float TEST_B = 60; const float TEST_C = TEST_A / TEST_B; int main() { std::cout << TEST_FLOAT << std::endl; std::cout << TEST_C << std::endl; std::cin.ignore(); return 0; } Result : 0 0.0166667 Tested on Visual Studio 2008 & 2010. I worked on other compilers that, if I remember well, made the first result like the second result. Now my memory could be wrong, but shouldn't TEST_FLOAT have the same value than TEST_C? If not, why? Is TEST_C value resolved at compile time or at runtime? I always

How to prevent optimization of busy-wait

做~自己de王妃 提交于 2019-12-02 03:37:42
问题 I need to have a function busy-wait. for(long int j=0; j<50000000; ++j) ; When I compile in release mode, this gets optimized out. Other than compiling in debug mode, is there some way to cause this to not get optimized out? I don't particularly care about the actual number of the loop, but it must be a noticeable busy-delay. 回答1: I don't know why you need to keep the CPU busy, but let's assume that you really have a good reason, like making sure you keep the CPU busy so it doesn't think

Map.get() optimization in ?: ternary operator

半世苍凉 提交于 2019-12-02 02:18:53
问题 Consider the following code: java.util.Map<String, String> map = new java.util.HashMap<String, String>(); ... String key = "A"; String value = map.get(key) == null? "DEFAULT_VALUE" : map.get(key); // (1) Would the compiler optimize the line (1) something similar to: String tmp = map.get(key); String value = tmp == null? "DEFAULT_VALUE" : tmp; (or to: String value = map.get(key); if(value == null) value = "DEFAULT_VALUE"; ) ? 回答1: Not sure if you are asking which corresponds to what the

Why Bother With the 'inline' Keyword in C++?

元气小坏坏 提交于 2019-12-02 01:18:29
I've just been researching the use and benefits/pitfalls of the C++ keyword inline on the Microsoft Website and I understand all of that. My question is this: if the compiler evaluates functions to see if inlining them will result in the code being more efficient and the inline keyword is only a SUGGESTION to the compiler, why bother with the keyword at all? EDIT: A lot of people are moaning about my use of __inline instead of inline . I'd like to point out that __inline is the Microsoft specific one: so it's not wrong, it's just not necessarily what you're used to. (Also fixed the website

How to prevent optimization of busy-wait

你离开我真会死。 提交于 2019-12-02 01:15:47
I need to have a function busy-wait. for(long int j=0; j<50000000; ++j) ; When I compile in release mode, this gets optimized out. Other than compiling in debug mode, is there some way to cause this to not get optimized out? I don't particularly care about the actual number of the loop, but it must be a noticeable busy-delay. I don't know why you need to keep the CPU busy, but let's assume that you really have a good reason, like making sure you keep the CPU busy so it doesn't think about that breakup it went through last week and get all depressed but I digress... The problem you are seeing