Tag: optimization

57 votes
15 answers
1.1K views
Quickly find whether a value is present in a C array?

I have an embedded application with a time-critical ISR that needs to iterate through an array of size 256 (pr

avatar
Sydney
0 1 0 9
asked 07 September, 2021
53 votes
1 answers
1K views
What is the best way to set a register to zero in x86 assembly: xor, mov or and?

All the following instructions do the same thing: set %eax to zero. Which way is optimal (requiring fewest mac

avatar
User demo
0 1 0 9
asked 07 September, 2021
50 votes
3 answers
1.8K views
Why does GCC generate such radically different assembly for nearly the same C code?

While writing an optimized ftol function I found some very odd behaviour in GCC 4.6.1. Let me show you the cod

avatar
Eddie
0 1 0 9
asked 07 September, 2021
56 votes
4 answers
3K views
Why would introducing useless MOV instructions speed up a tight loop in x86_64 assembly?

Background: While optimizing some Pascal code with embedded assembly language, I noticed an unnecessary MOV i

avatar
Jesenia
0 1 0 9
asked 07 September, 2021
58 votes
11 answers
1.9K views
Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

I wrote these two solutions for Project Euler Q14, in assembly and in C++. They implement identical brute forc

avatar
Deedee
0 1 0 9
asked 07 September, 2021
64 votes
11 answers
1.9K views
Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs

I was looking for the fastest way to popcount large arrays of data. I encountered a very weird effect: Changin

avatar
Daniel
0 1 0 9
asked 07 September, 2021
58 votes
12 answers
930 views
Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optim

avatar
Venice
0 1 0 9
asked 07 September, 2021
67 votes
30 answers
1.2K views
Fastest way to list all primes below N

This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes

avatar
Alba
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
1K 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

avatar
Margo
0 1 0 9
asked 07 September, 2021
51 votes
9 answers
2.7K views
Swift Beta performance: sorting arrays

I was implementing an algorithm in Swift Beta and noticed that the performance was very poor. After digging de

avatar
Basil
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.2K views
Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which i

avatar
Lynette
0 1 0 9
asked 07 September, 2021