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
8 answers

Efficient code with loops and types C#

I wanted to find out what more efficient (if even) in the following code Value type ForEach(string s in strings) { string t = s; } // or string t; ForEach(string s in strings) { t = s; } and is there a different with reference types.
guyl
  • 2,158
  • 4
  • 32
  • 58
0
votes
1 answer

Is there a penalty in having a non-aligned Jcc which is nearly never taken in Intel/AMD 64?

I have a loop I use to add numbers with carry. I'm wondering whether having .done: align would give me anything? After all, it will branch there only once per call to the function. I know that a C compiler is likely to align all branches affected by…
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
0
votes
1 answer

Is there any way to schedule an event without constantly running checks

Let's say I want to run a function when certain conditions are met, for example I want to show a pop up message at a certain hour. Do I have to keep checking every so often or is there any other way to schedule an event without having to have a…
Paskky
  • 11
  • 1
  • 2
0
votes
1 answer

Backing field vs "value" keyword in property setter

I have a property with a backing field and some logic inside the setter. I wonder whether I should use the value keyword or the backing field. Option 1: private bool _backingField; public bool MyProperty { get => _backingField; set { …
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
0
votes
1 answer

How can I find out the time required to perform of a particular instruction for Xtensa microprocessor for e.g. wsr / rsr?

I am trying to optimize a code on esp32 which uses xtensa LX6 microprocessors , I wanted to know the cost of wsr and rsr instructions which are used to read or wirte in the special registers .
0
votes
3 answers

Micro-optimisation of keys versus in_array() in PHP

So you have the option to structure an array as you please knowing that a few lines further down in your code you are going to need to check for the existence of a value in that array. The way I see it you have at least two options: $values_array =…
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
0
votes
1 answer

Optimal List Comprehension (Filtering Existing List)

I have a large list (1e8+ entries) in the form [index:boolean]. I want to find the indices of the values that are True. Performance in this task is of the utmost importance. Currently from what I can tell in Python 3.7.2, the optimal way of doing…
turbo_sheep
  • 109
  • 2
  • 7
0
votes
2 answers

C# How do basic operation time vary with the size of the numbers?

Context of this is a function, which needs to run pretty much once per frame, and is therefore very critical performance-wise. This function contains a loop, and operations inside it. private int MyFunction(int number) { // Code for (int i =…
0
votes
0 answers

How to traverse an array of 50k accounts and display them on leaflet map?

I have 50k accounts that i have to display on a map as markers. Everything is fine if i call only for maximum 20k accounts but for a larger number of accounts page become unresponsive. I figure out that it take too long to go through all the array …
cUser
  • 392
  • 8
  • 25
0
votes
0 answers

Does PHP optimize multiple strlen calls on for loop?

I am trying to make a PHP function that replaces all occurrences between two strings for an another given string. I am almost sure I have accomplished what I was looking for, however while I was programming I had two versions of it. I would like you…
Exprove
  • 1,301
  • 2
  • 18
  • 32
0
votes
1 answer

JavaScript: Faster roulette selection

I am implementing a selection algorithm that selects an object based on a probability proportional to its score value. This makes higher-scoring objects more likely to be chosen. My implementation is as follows: var pool = []; for (var i = 0; i <…
w0f
  • 908
  • 9
  • 23
0
votes
1 answer

Using RSI/RDI vs r8-r15 (speed optimization)

I want to optimize my function as much as possible and one of the things I did was use r8 as a pointer because that's the register the pointer gets pushed into in x64 funtions. But would pushing RSI or RDI, moving the pointer to them and using them…
b.sullender
  • 163
  • 1
  • 16
0
votes
2 answers

Java Micro-optimization: To cache or not to cache a System.currentTimeMillis() return value?

Simple question, which I've been wondering. Of the following two versions of code, which is better optimized? Assume that the time value resulting from the System.currentTimeMillis() call only needs to be pretty accurate, so caching should only be…
Kael Eppcohen
  • 177
  • 1
  • 2
  • 10
0
votes
1 answer

Is there a loop construct that repeats n times without calculating some conditional?

This question arose out of the context of optimizing code to remove potential branch prediction failures.. in fact, removing branches all together. For my example, a typical for-loop uses the following syntax: #include #include…
0
votes
1 answer

Check if buffer was used in last query

I was wondering whether there is any query/config/trick/etc to know if the innodb_buffer was used in the fetching of result for last query. PS: This is in context of performance tuning, and I don't want to keep things to best guesses, so is there…
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88