micro-optimization

How efficient is an if statement compared to a test that doesn't use an if? (C++)

I need a program to get the smaller of two numbers, and I'm wondering if using a standard "if x is less than y" int a, b, low; if (a < b) low = a; else low = b; is more or less efficient than this: int a, b, low; low = b + ((a - b) & ((a - b) >> 31)); (or the variation of putting int delta = a - b at the top and rerplacing instance...

Duplicate literals and hard-coding.

I see the follow pattern occurring quite frequently: b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); Notice that the literal string is used twice. The extract is from the nginx source-base. The compiler should be able to merge these literals when it is encountered within the compilation unit. My questions a...

Intel 8086 Assembly -- Squaring a Register

In principle, squaring the value of a register isn't hard: mov ax, [var] mov cx, [var] mul cx // square of answer is in DX:AX But I got to thinking -- the course that I'm learning Assembly for prizes efficiency very highly; a difference of even a single line less could be worth up to 5 points. I realize this is micro-optimiza...

How to test what method implementation runs faster

While the question check if input is type of string has been closed two of the answers spiked a micro-optimization question in my mind: which of the below two solutions would perform better? Reed Copsey provided a solution using Char.IsLetter: string myString = "RandomStringOfLetters"; bool allLetters = myString.All( c => Char.IsLetter...

Two loop bodies or one (result identical)

I have long wondered what is more efficient with regards to making better use of CPU caches (which are known to benefit from locality of reference) - two loops each iterating over the same mathematical set of numbers, each with a different loop body, or having one loop that "concatenates" the two bodies into one, and thus accomplishes id...

Question about loop speed

I have the following two loops: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main(){ int start=clock(); for (int i=0;i<100;i++) cout<<i<<" "<<endl; cout<<clock()-start<<"\n"<<endl; cout<<"\n"; int start1=clock(); for (int j=0;j<100;++j) cout<<j<<" "<<endl; ...

optimizing a lookup

Hi, I have an array that i use to lookup values. I use the first 2 values to get n rows. for example all rows that have 2 in the first column and 7 in the second. What is the fastest (i mean micro-optimized) way to get these values? I now use a for loop to get the values: int l = SpreadLookupTable.GetLength(0); for (int iCombo = 0; ...

Is there any point in creating a second column optimized for FULLTEXT searches?

Hi, the project I'm working on has for each column that needs to be searched a second column called "ft[columnname]" which has a FULLTEXT index and only this one is searched against. This column contains an "optimized" text, that is automatically generated from the original column in the following way: The string is lowercased All acc...

Micro-optimizations: using intptr_t for flag/bool types.

From what I understand, the definition of intptr_t varies by architecture -- it is guaranteed to have the capacity to represent a pointer that can access all of the uniform address space of a process. Nginx (popular open source web-server) defines a type that is used as a flag(boolean) and this a typedef to intptr_t. Now using the x86-6...

Is it possible to have only one comparison per iteration of a binary search algorithm?

In binary search algorithm we have two comparisons: if (key == a[mid]) then found; else if (key < a[mid]) then binary_search(a[],left,mid-1); else binary_search(a[],mid+1,right); Is there a way by which I can have only one comparison instead of the above two. -- Thanks Alok.Kr. ...

Use of lazy val for caching string representation

I encountered the following code in JAXMag's Scala special issue: package com.weiglewilczek.gameoflife case class Cell(x: Int, y: Int) { override def toString = position private lazy val position = "(%s, %s)".format(x, y) } Does the use of lazy val in the above code provide considerably more performance than the following code? ...

Is it worth writing part of code in C instead of C++ as micro-optimization?

I am wondering if it is still worth with modern compilers and their optimizations to write some critical code in C instead of C++ to make it faster. I know C++ might lead to bad performance in case classes are copied while they could be passed by reference or when classes are created automatically by the compiler, typically with overloa...