Optimization is the act of improving a method or design. In programming, optimization usually takes the form of increasing the speed of an algorithm, or reducing the resources it requires.
322
votes
33answers
31k views
Performance optimization strategies of last resort
There are plenty of performance questions on this site already, but it occurs to me that almost all are very problem-specific and fairly narrow. And almost all repeat the advice to avoid premature ...
4774
votes
11answers
246k views
Why is processing a sorted array faster than an unsorted array?
Here is a piece of C++ code that shows some very peculiar performance. For some strange reason, sorting the data miraculously speeds up the code by almost 6x:
#include <algorithm>
#include ...
109
votes
14answers
12k views
Is there a performance difference between i++ and ++i in C++?
We looked at this answer for C in this question:
http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c
What's the answer for C++?
111
votes
17answers
75k views
Most effective way for float and double comparison
What would be the most efficient way to compare two doubles or two floats (single precision)?
Simply doing this is not correct:
bool CompareDoubles1 (double A, double B)
{
return A == B;
}
But ...
28
votes
17answers
16k views
One could use a profiler, but why not just halt the program? [closed]
If something is making a single-thread program take, say, 10 times as long as it should, you could run a profiler on it. You could also just halt it with a "pause" button, and you'll see exactly what ...
117
votes
19answers
20k views
Fastest way to list all primes below N in python
This is the best algorithm I could come up with after struggling with a couple of Project Euler's questions.
def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
...
244
votes
22answers
89k views
Big O, how do you calculate/approximate it?
Most people with a degree in CS will certainly know what Big O stands for.
It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying ...
44
votes
12answers
11k views
Tips for optimizing C#/.NET programs [closed]
It seems like optimization is a lost art these days. Wasn't there a time when all programmers squeezed every ounce of efficiency from their code? Often doing so while walking 5 miles in the snow?
In ...
53
votes
22answers
25k views
Advantage of switch over if-else statement
What's the best practice for switch vs if for a 30 unsigned enumerations where about 10 have an expected action (that presently is the same action). Performance and space need to be considered but ...
400
votes
28answers
71k views
Fastest way to determine if an integer's square root is an integer
I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer). I've done it the easy way, by using the built-in Math.sqrt() function, but ...
149
votes
11answers
23k views
Is there a performance difference between i++ and ++i in C?
Is there a performance difference between i++ and ++i if the resulting value is not used?
120
votes
9answers
32k views
Tool to identify unused css definitions [closed]
Are there any good tools to help identify unused css definitions in project? A bunch of css files were pulled in and now I'm trying to clean things up a bit.
69
votes
10answers
42k views
What's the best string concatenation method using C#?
What's the most efficient way to concatenate strings?
39
votes
18answers
7k views
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?
for (;;) {
//Something to be done repeatedly
}
I have seen this sort of thing used a lot, but I think it is rather strange...
Wouldn't it be much clearer to say while(true), or something along ...
42
votes
9answers
18k views
MYSQL OR vs IN performance
I am wondering if there is any difference in regards to performance between the following
SELECT ... FROM ... WHERE someFIELD IN(1,2,3,4)
SELECT ... FROM ... WHERE someFIELD between 0 AND 5
SELECT ...