U
U
Urukhayy2015-04-03 10:29:50
Programming
Urukhayy, 2015-04-03 10:29:50

Is the presence of two identical constants in the code a sign that the solution is not optimal?

Is the presence of two identical constants in the code a sign that the solution is not optimal?
And what if such duplicate constants take up more than 1.5 kilobytes of RAM?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mrrl, 2015-04-03
@Urukhayy

Depends on the length of the constant. Which is shorter - load it into a register directly in the command, or take it from memory (by specifying the address in the command)? Highly dependent on the processor.
In general, the same constants in the algorithm are not scary. Worse, when the same condition has to be checked many times. Or, even worse, conditions that follow one from the other (we checked that a < b, and then we check that a + c < b + c). This is often seen in geometry. And in this case, it is worth working on the algorithm.

K
KorsaR-ZN, 2015-04-03
@KorsaR-ZN

What is this 1.5 KB constant?

V
Vitaly Pukhov, 2015-04-03
@Neuroware

What kind of constant is over 1.5 kilobytes?
And if, in fact, if it is used often, then it is better to combine them, because. if it is alone, it will get into the cache more often and, accordingly, the performance will be higher, if there are 2 of them, it will overwrite in the cache and it will take more time to return it to the cache

E
Eddy_Em, 2015-04-03
@Eddy_Em

I will name at least 1 case when this is necessary: ​​if you store some arrays of constants in the microcontroller and at the same time want to be able to "rollback" to "factory". In this case, one array is overwritten and stored either in EEPROM or page-aligned flash (if you have a small controller without an EEPROM). And the second one is simply placed as the compiler pleases.
But if you write for a computer, then you already need to figure it out, perhaps such duplication will also make some sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question