Skip to main content
154 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
  • 38k
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,589
130 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.3k
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
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
61 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
  • 136k
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
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
  • 81.4k
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
  • 218k
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 supports Canada's user avatar
37 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
  • 5,175
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
  • 58.1k
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,062
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
  • 136k
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
28 votes

What are the benefits of multi-file programming?

The question falls into same category as why buildings are not build from one piece of rock but a bunch of bricks? Answer: easier to navigate than scroll through one huge file make recompile works ...
Polar Bear's user avatar
28 votes
Accepted

Accessing enemies' locations quickly in a 2D game

For this kind of domain, it is is not uncommon that you need bidirectional lookup: quick access to the coordinate of an enemy (or more general, of any kind of piece) quickly determine which piece is ...
Doc Brown's user avatar
  • 218k
27 votes
Accepted

Can "sub-linear" still be a straight line?

No, it cannot be. It looks like it can from this graph because it is a log-log plot, which means both the x and y axes are compressed. Any function which satisfies the relation y = a*x^c for some ...
David Etler's user avatar
26 votes

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

If a certain level of performance is necessary (a non-functional requirement), then that should be a design goal from the start. E.g. this can influence which technologies might be appropriate, or how ...
amon's user avatar
  • 136k
22 votes

When do function call costs still matter in modern compilers?

Almost all adages about tuning code for performance are special cases of Amdahl's law. The short, humorous statement of Amdahl's law is If one piece of your program takes 5% of runtime, and you ...
zwol's user avatar
  • 2,620
21 votes

What are the benefits of multi-file programming?

The other answers are fine, but something they're missing is actual technical limitations. For example, you can't actually save all of the code for my day-job application in one file - it's bigger ...
Telastyn's user avatar
  • 110k
20 votes

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

You should remove this consultant from touching any of your system. A consultant suggesting a complete rewrite of an application in a different language are going to cause a lot of damage to the ...
Lie Ryan's user avatar
  • 12.5k
19 votes

Better solutions than joining table for Many to Many?

I could query the joining table for this information but I imagine queries would be slow since there's no clear way to horizontally partition the data. Assuming the Primary key on this table is { ...
Phill  W.'s user avatar
  • 13k

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