Questions tagged [micro-optimization]

Micro-optimization is the process of meticulous tuning of small sections of code in order to address a perceived deficiency in some aspect of its operation (excessive memory usage, poor performance, etc).

Micro-optimization is the process of meticulous tuning of small sections of code in order to address a perceived deficiency in some aspect of its operation (excessive memory usage, poor performance, etc).

Micro-optimization (and optimization in general) tends to be interesting to programmers because they enjoy finding clever solutions to problems. However, micro-optimization carries the connotation of a disproportionate amount of effort being expended to extract relatively small improvements.

That's not to say that micro-optimization is bad practice in all circumstances. Sometimes a small improvement in a part of a code base that gets used frequently (such as the innermost part of a loop) can yield big overall gains in system performance, and building code for highly constrained systems such as microcontrollers will often require cleverness to eke out the most performance from such a small system.

However, it can be tempting to indulge in the practice where it's not necessary, resulting in a lot of time being spent that could have been used more productively, and in code that is difficult to follow as "clever" solutions to problems are often more difficult to understand than simple solutions, and therefore a micro-optimization can have a negative impact on the maintainability of a piece of code.

Programmers are advised to avoid micro-optimization, unless they can make a solid justification for the problems outlined above being worth the performance gains. Should profiling of the code in question identify a hot-spot that is causing a performance bottleneck, then this can be sufficient justification for a micro-optimization.

900 questions
0
votes
0 answers

Why is increment ($x++) slower than assignment ($x = $x + 1) in PHP 5.5.9?

I made the following performance test for PHP. I am trying to profile the time it takes to increment a variable in four ways, in a array $time: [{++$x}, {$x++}, {$x += 1}, {$x = $x + 1}] $time = [0, 0, 0, 0]; for($i = 0; $i < 50; ++$i){ $k = 0; …
pixunil
  • 641
  • 1
  • 5
  • 19
0
votes
0 answers

Does comparison to primitives in functions lead to GC events?

When we compare something to primitives in functions, are those primitives created and then garbage-collected or browsers do some optimizations on that? It seems such a basic case to me that I wonder if it makes sense to micro-optimize, e.g. assign…
Oleg
  • 9,341
  • 2
  • 43
  • 58
0
votes
1 answer

How to assign columns (and column ranges) to separate array variables?

Suppose I have the following (newline-separated) array in Bash (4.3): abc def ghi jkl # ${myArray[0]} abc def ghi jkl mno # ${myArray[1]} abc def ghi # ${myArray[2]} ... I would like to split this up columnwise, into…
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
0
votes
2 answers

php syntax for set variable by existing or new

Another micro-optim question. What is the php best way to set variable like in javascript existing or new value var a = a || false using $a = $a || false; we get error notice abour undefined variable $a. This variable may or may not be set in…
Jirka Kopřiva
  • 2,939
  • 25
  • 28
0
votes
4 answers

Decimal concatenation of two integers using bit operations

I want to concatenate two integers using only bit operations since i need efficiency as much as possible.There are various answers available but they are not fast enough what I want is implementation that uses only bit operations like left shift,or…
Naseer
  • 4,041
  • 9
  • 36
  • 72
0
votes
1 answer

Should I Calculate Time Values or Declare Them At The Start of My Code?

TL;DR: Whats better? cookie_expires = time()+( (60*60*24)*31 ) vs. cookie_expires = SECONDS_month To save time and typing, for years I have been declaring useful time values at the start of my apps. SECONDS_hour = 3600 SECONDS_day =…
ethanpil
  • 2,522
  • 2
  • 24
  • 34
0
votes
1 answer

Non conditional boolean to int conversion java - micro optimisation

I'm fairly new to Java and I'm writing an android game - and there's one line of code in a tight loop that's really annoying me. targetBlocksRemain += letterArray[column][row].isTargetBlock() ? 1 : 0; I feel like must be possible to write this…
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
0
votes
4 answers

Differences between mult and div operations on floating point numbers

Is any difference in computation precision for these 2 cases: 1) x = y / 1000d; 2) x = y * 0.001d; Edit: Shoudn't add C# tag. Question is only from 'floating-point' point of view. I don't wanna know what is faster, I need to know what case will give…
apocalypse
  • 5,764
  • 9
  • 47
  • 95
0
votes
2 answers

Fast way to find an ascii character in long integers using bitwise operations?

I'm making a strchr implementation and trying to optimize my code to use the 64bit properties of my machine. So, I' converting my strings to long integers, thus comparing 8 chars at a time. Currently, I have : int has_char(unsigned long word,…
achedeuzot
  • 4,164
  • 4
  • 41
  • 56
0
votes
0 answers

Page request and memory optimization in PHP/APACHE

Let's say in my website I have user & admin .php pages. the user page accommodates requests made by users, which may be 5000 pageviews a day, but my admin page gets only 100 page views (from me) per day. Now, I was thinking being able to access my…
samayo
  • 16,163
  • 12
  • 91
  • 106
0
votes
3 answers

define variable inside vs outside in Java?

Suppose we have a double loop with a large number of iteration, should we define the variable outside the loop for speed up? Just for example: for(int i=0;i<2000;i++) for(int j=0;j<1000;j++) System.out.println(i+j); Since we…
ohmygoddess
  • 619
  • 1
  • 7
  • 23
0
votes
2 answers

PHP performance on checking if all array elements are of the same type

I've stumbled on what would be the fastest way to check whether or not all elements in an array are of the same type, in PHP. After browsing StackOverflow for different methods to do this, I decided to check which of them is faster. I've tested…
Claudi
  • 5,224
  • 17
  • 30
0
votes
3 answers

word alignment of 4 byte for XOR operations

Is there any advantage in doing bitwise operations on word boundaries? Any CPU or memory optimization in doing so? Actual problem: I am trying to create XOR of two structure. Lets say structure-1 and structure-2 both of same size 10000 bytes. I…
Jack
  • 1,398
  • 3
  • 16
  • 26
0
votes
1 answer

Microoptimizing D jagged arrays

Is there a reason to linearize multidimensional arrays into flat for productivity? I mean, even hypothetically you just take pointer operations from compiler and do them explicitly when counting indexes. So what's the point?
akalenuk
  • 3,815
  • 4
  • 34
  • 56
0
votes
2 answers

Are empty blocks run?

This is relevant, b.c. I want to test looping structures. What I normally do is put in a simple statement like i++ in the loop. I do this b.c. I wonder if a smart interpreter will not run empty blocks. For example. for(var i = 0; i < 10; i++)…
user1637281