Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

From MS Partition I, section 12.1 (Supported data types): The signed integer types (int8, int16, int32, int64, and native int) and their corresponding unsigned integer types (unsigned int8, unsigned int16, unsigned int32, unsigned int64, and native unsigned int) differ only in how the bits of the integer are interpreted. For those operations in which an … Read more

Why can’t GCC generate an optimal operator== for a struct of two int32s?

If you “fix” the alignment, all give the same assembly language output (with GCC): struct alignas(std::int64_t) Point { std::int32_t x, y; }; Demo As a note, some correct/legal ways to do some stuff (as type punning) is to use memcpy, so having specific optimization (or be more aggressive) when using that function seems logical.

When, if ever, is loop unrolling still useful?

Loop unrolling makes sense if you can break dependency chains. This gives a out of order or super-scalar CPU the possibility to schedule things better and thus run faster. A simple example: for (int i=0; i<n; i++) { sum += data[i]; } Here the dependency chain of the arguments is very short. If you get … Read more

Why does my application spend 24% of its life doing a null check?

The tree is massive By far the most expensive thing a processor ever does is not executing instructions, it is accessing memory. The execution core of a modern CPU is many times faster than the memory bus. A problem related to distance, the further an electrical signal has to travel, the harder it gets to … Read more

Cost of exception handlers in Python

Why don’t you measure it using the timeit module? That way you can see whether it’s relevant to your application. OK, so I’ve just tried the following: import timeit statements=[“””\ try: b = 10/a except ZeroDivisionError: pass”””, “””\ if a: b = 10/a”””, “b = 10/a”] for a in (1,0): for s in statements: t … Read more

Should I use Java’s String.format() if performance is important?

I took hhafez code and added a memory test: private static void test() { Runtime runtime = Runtime.getRuntime(); long memory; … memory = runtime.freeMemory(); // for loop code memory = memory-runtime.freeMemory(); I run this separately for each approach, the ‘+’ operator, String.format and StringBuilder (calling toString()), so the memory used will not be affected by … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)