Questions tagged [optimization]

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. Another meaning of optimization is numerical optimization algorithms used in machine learning.

21440
votes
21answers
1.2m views

Why is it faster to process a sorted array than an unsorted array?

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster.#include <algorithm>#include <ctime>#...
253
votes
4answers
42k views

What are copy elision and return value optimization?

What is copy elision? What is (named) return value optimization? What do they imply?In what situations can they occur? What are limitations?If you were referenced to this question, you're probably ...
341
votes
38answers
89k views

Flatten an irregular list of lists

Yes, I know this subject has been covered before (here, here, here, here), but as far as I know, all solutions, except for one, fail on a list like this:L=[[[1, 2, 3], [4, 5]], 6]Where the ...
558
votes
34answers
68k views

Performance optimization strategies of last resort [closed]

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 ...
3630
votes
51answers
2.1m 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 is better, in terms of functionality, page load speed, validation purposes, etc.?...
429
votes
25answers
305k views

What is the most effective way for float and double comparison?

What would be the most efficient way to compare two double or two float values?Simply doing this is not correct:bool CompareDoubles1 (double A, double B){return A==B;}But something like:...
778
votes
22answers
362k 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 ...
346
votes
32answers
83k views

What is the most “pythonic” way to iterate over a list in chunks?

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list ...
306
votes
30answers
154k 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=[]while numbers:p=numbers.pop()primes.append(p)numbers....
2540
votes
10answers
337k views

Improve INSERT-per-second performance of SQLite?

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second!Background: We are using SQLite as part of a desktop ...
84
votes
1answer
26k 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 machine cycles)?xorl %eax, %eaxmov $0, %eaxandl $0, %eax
35
votes
7answers
12k views

How to improve performance of this code?

Thanks to some help from people here, I was able to get my code for Tasmanian camels puzzle working. However, it is horribly slow (I think. I'm not sure because this is my first program in python). ...
43
votes
17answers
30k 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 ...
728
votes
11answers
131k views

Why is this C++ code faster than my hand-written assembly for testing the Collatz conjecture?

I wrote these two solutions for Project Euler Q14, in assembly and in C++. They are the same identical brute force approach for testing the Collatz conjecture. The assembly solution was assembled with...
69
votes
20answers
13k views

When is optimisation premature?

As Knuth said,We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.This is something which often comes up in Stack Overflow ...
32
votes
4answers
17k views

Fastest way to do horizontal float vector sum on x86

You have a vector of three (or four) floats. What is the fastest way to sum them?Is SSE (movaps, shuffle, add, movd) always faster than x87? Are the horizontal-add instructions in SSE4.2 worth it? ...
1175
votes
29answers
225k 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 ...
151
votes
18answers
84k views

A weighted version of random.choice

I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with:def weightedChoice(choices):"""Like ...
432
votes
11answers
189k views

Getting all types that implement an interface

Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations?This is what I want to re-write:foreach (Type t in this....
190
votes
20answers
100k views

Declaring variables inside or outside of a loop

Why does the following work fine?String str;while (condition) {str=calculateStr();.....}But this one is said to be dangerous/incorrect:while (condition) {String str=...
68
votes
2answers
6k views

What exactly is the “as-if” rule?

As the title says, What exactly is the "as-if" rule? An typical answer one would get is: The rule that allows any and all code transformations that do not change the observable ...
3
votes
2answers
4k views

Swing animation running extremely slow

I have a problem with my current animation that I'm running using Java Swing. It is a discrete event simulation and the text based simulation is working fine, I'm just having problems connecting the ...
142
votes
12answers
61k views

MYSQL OR vs IN performance

I am wondering if there is any difference in regards to performance between the followingSELECT ... FROM ... WHERE someFIELD IN(1,2,3,4)SELECT ... FROM ... WHERE someFIELD between 0 AND 5SELECT ...
77
votes
3answers
73k views

How do I choose grid and block dimensions for CUDA kernels?

This is a question about how to determine the CUDA grid, block and thread sizes. This is an additional question to the one posted here:http://stackoverflow.com.mevn.net/a/5643838/1292251Following this ...
179
votes
12answers
108k views

Where to place JavaScript in an HTML file?

Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or so. By file I mean it’s an external file that would be linked in via <script src="http://stackoverflow.com/...">, not pasted into the HTML ...
227
votes
17answers
164k views

Most efficient way to concatenate strings?

What's the most efficient way to concatenate strings?
98
votes
26answers
100k views

What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?

If I have some integer n, and I want to know the position of the most significant bit (that is, if the least significant bit is on the right, I want to know the position of the furthest left bit that ...
111
votes
2answers
17k views

Meaning of acronym SSO in the context of std::string

In a C++ question about optimization and code style, several answers referred to "SSO" in the context of optimizing copies of std::string. What does SSO mean in that context?Clearly not "single sign ...
294
votes
16answers
68k views

Should import statements always be at the top of a module?

PEP 08 states:Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.However if the class/method/function that I am ...
72
votes
11answers
36k 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 five miles in the snow?...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,