152 votes
Accepted

When do function call costs still matter in modern compilers?

It depends on your domain. If you are writing code for low-power microcontroller, then method call cost might be significant. But if you are creating normal website or application, then method call ...
Euphoric's user avatar
  • 37.4k
146 votes
Accepted

When to optimize for memory vs performance speed for a method?

Instead of speculating about what may or may not happen, let's just look, shall we? I'll have to use C++ since I don't have a C# compiler handy (though see the C# example from VisualMelon), but I'm ...
Phil Frost's user avatar
  • 1,569
131 votes
Accepted

Is it bad practice to write code that relies on compiler optimizations?

Employ the least astonishment principle. Is it you and only ever you who is going to use this code, and are you sure the same you in 3 years is not going to be surprised by what you do? Then go ...
Pieter B's user avatar
  • 13.2k
108 votes
Accepted

Can we make general statements about the performance of interpreted code vs compiled code?

No. In general, the performance of a language implementation is primarily dependent on the amount of money, resources, manpower, research, engineering, and development spent on it. And specifically, ...
Jörg W Mittag's user avatar
82 votes

Is it bad practice to write code that relies on compiler optimizations?

For this particular case, definitely just return by value. RVO and NRVO are well-known and robust optimizations that really ought to be made by any decent compiler, even in C++03 mode. Move semantics ...
Quentin's user avatar
  • 1,475
80 votes

Can we make general statements about the performance of interpreted code vs compiled code?

Generalizations and specific scenarios are literally opposites. You seem to be contradicting yourself. On the one hand, you want to make a general statement about interpreted vs compiled languages. ...
loneboat's user avatar
  • 971
73 votes
Accepted

Using a different algorithm depending on the size of the input

Why don't we just use a different algorithm based on the size of the input? We do. Hybrid algorithms are used all the time. Why shouldn't/couldn't I make a function like this (written in pseudo-C#-...
Jörg W Mittag's user avatar
71 votes

What are the benefits of multi-file programming?

There are a lot of technical reasons behind using multiple files when writing large complex systems. All of them are meaningless in the face of the best reason to use multiple files: Readability. ...
candied_orange's user avatar
66 votes

When to optimize for memory vs performance speed for a method?

To answer the stated question: When to optimize for memory vs performance speed for a method? There are two things you have to establish: What is limiting your application? Where can I reclaim the ...
Berin Loritsch's user avatar
63 votes

Is it bad practice to write code that relies on compiler optimizations?

Now, I feel that the syntax is much clearer when the object is explicitly returned by value, and the compiler will generally employ the RVO and make the process more efficient. Is it bad practice to ...
Barry's user avatar
  • 1,308
62 votes
Accepted

In software programming, would it be possible to have both CPU and GPU loads at 100%?

Theoretically yes, but practically it's rarely worth it. Both CPUs and GPUs are turing-complete, so any algorithm which can be calculated by one can also be calculated by the other. The question is ...
Philipp's user avatar
  • 23.3k
60 votes

When do function call costs still matter in modern compilers?

Function call overhead depends entirely on the language, and at what level you are optimizing. On an ultra low level, function calls and even more so virtual method calls may be costly if they lead ...
amon's user avatar
  • 134k
60 votes
Accepted

Does Garbage Collection Scan The Entire Memory?

I was reading a bit about garbage collectors and I am wondering if the garbage collector of a program scans the entire heap memory or what is allocated to it? That depends on the garbage collector. ...
Jörg W Mittag's user avatar
53 votes

When is it better to optimize a software for better performance, at the beginning or at the end of the development?

The number one thing should always and forever be readability. If it's slow but readable, I can fix it. If it's broken but readable, I can fix it. If it's unreadable, I have to ask someone else what ...
candied_orange's user avatar
52 votes
Accepted

What is a realistic, real-world, maximum size for a SQLite database?

The realistic limit (of the size of some Sqlite database) is the same as the realistic limit for a data file. And that limit depends a lot of your computer & system. On my current Linux desktop I ...
Basile Starynkevitch's user avatar
51 votes

Am I right that switching programming languages will have little impact on the scalability of a CRUD API?

At a global level, you're wrong - language does matter, or at the very least you will spend more $$$ on compute if you write it in a less computationally efficient language. While I'm not at liberty ...
Philip Kendall's user avatar
50 votes

Working through the single responsibility principle (SRP) in Python when calls are expensive

Many potential performance concerns are not really a problem in practice. The issue you raise may be one of them. In the vernacular, we call worrying about those problems without proof that they are ...
Robert Harvey's user avatar
48 votes
Accepted

Better solutions than joining table for Many to Many?

Quote of the day: Premature optimization is the root of all evil- C.A.R. Hoare There is no reason to assume that the join will be slow, if you made sure that the join table is indexed on its both ...
Christophe's user avatar
  • 77.2k
45 votes

When to optimize for memory vs performance speed for a method?

"this would reduce memory" - em, no. Even if this would be true (which, for any decent compiler is not), the difference would most probably be negligible for any real world situation. However, I ...
Doc Brown's user avatar
  • 207k
42 votes
Accepted

Am I right that switching programming languages will have little impact on the scalability of a CRUD API?

There's a good video (warning, contains really a lot of swearing, but worth it) that will give you a good basis for arguing against this consultant... Solving Imaginary Scaling Issues (at Scale), by ...
user3067860's user avatar
40 votes
Accepted

What are the complexities of a binary search?

The full term for this is "Time Complexity" and you'll want to use that if you are searching. This is a core concept in computer science. I am not going to attempt to explain it in detail ...
JimmyJames's user avatar
  • 27.3k
37 votes

Can we make general statements about the performance of interpreted code vs compiled code?

As a rule of thumb, an interpreted program is about 2x–10x slower than writing the program in the interpreter's host language, with interpreters for more dynamic languages being slower. This is ...
amon's user avatar
  • 134k
36 votes
Accepted

How to deal with misconceptions about "premature optimization is the root of all evil"?

It seems you are looking for shortcuts not to try out the "purest naive implementation" first, and directly implement a "more sophisticated solution because you know beforehand that the naive ...
Doc Brown's user avatar
  • 207k
36 votes

In software programming, would it be possible to have both CPU and GPU loads at 100%?

It is not related to game programming. Some scientific code can also use both the GPU and the CPU. With careful -and painful- programming, e.g. by using OpenCL or CUDA, you could load both your GPU ...
Basile Starynkevitch's user avatar
36 votes
Accepted

Why should I use foreign keys in database?

It maintains referential integrity (yes but can be maintained without it too) You are technically correct that if you're able to maintain referential integrity yourself, you don't need the constraint ...
Flater's user avatar
  • 49.8k
36 votes

Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?

Don't we need to take a step back here? Under the hood, it generally all boils down to simply functions being called the with the this pointer as first arg. It's good to question things from first ...
Alexander's user avatar
  • 4,884
33 votes

When to optimize for memory vs performance speed for a method?

You can do better than both of those with return (abs(a + b) > 1000); Most processors (and hence compilers) can do abs() in a single operation. You not only have fewer sums, but also fewer ...
Graham's user avatar
  • 2,006
31 votes
Accepted

Is code written inline faster than using function calls?

Many language implementations will automatically inline function calls wherever this makes sense and is possible. This is completely normal for “compiled languages” like C, or JIT-compiling runtimes ...
amon's user avatar
  • 134k
28 votes
Accepted

How much do function calls impact performance?

Maybe. The compiler might decide "hey, this function is only called a few times, and I'm supposed to optimize for speed, so I'll just inline this function". Essentially, the compiler will replace ...
CHendrix's user avatar
  • 514
28 votes
Accepted

how many cores should I utilize for calculations? #cores or #cores -1?

Major operating systems are mature enough to know how to handle processes which use every available core. Other processes may (and often will) be affected, but the computation won't become slower ...
Arseni Mourzenko's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible