Is there such a Thing as a Program Optimizer?

Is there such a Thing as a Program Optimizer?

After you have gotten your program to work fine in what ever language (C for me right now), is there a Second compiler you can put it through that it would search for code that would do the same thing with less code and replace the main code with the shorter code and make things run faster?

It is a silly idea, but it seems it would certainly save some time and be very useful, at least to me.

If they make such an animal, what would it be called?

It would be called 'Brain' and is quite common nowadays.

Of course it is not easy to use at first as it doesn't come with a man page and refuses to work sometimes, but the more you use it the better it gets. Legend has it that you can improve its effectiveness by a cup of coffee from time to time. Good chocolate may work too for some versions of it.

I'll opt for the hot chocolate.

The Brain is indeed a good utility to use, but the compiles is actually very good at optimizing code.

would you know where I can get an optimizer for the arduino language?

would you know where I can get an optimizer for the arduino language?

"the arduino language" is C/C++. The compiler used by the Arduino development environment are the GNU compilers for these languages, "gcc". "gcc" does a good job of optimizing and will probably come reasonably close to giving you perfectly optimized output for a given program you tell it to compile.

That said -- it's still just a computer program, it has no human judgement. If you tell it to do something 50 times when you only need it to happen 10 times it doesn't know that -- and your program will by 5x slower because of it. If you tell it to sort a list of numbers because you want to know what the biggest one is then it will waste a ton of time sorting that list for you, obedient to your instruction, when all you actually needed was to walk through the list and find the biggest number.

I can't say it strongly enough: you are far more likely to be the cause of your program running slowly than any of the tools you are using.

Thank You, that is good to know. I have just finished a working unit this AM. I am begging right now to make it faster. I just thought I would check and see if such a program could make this job easier.

I am not a 'c' programmer. I have programmed in other languages. I am starting to Love 'c' the more I work with it.

Sincerely,

:slight_smile:

The compiler should go through and take care of the really mathematically obvious ones. Like variables that have some math done on them that never gets used at all will probably get optimized out (probably. If you see it happen remove it, don't rely on this).

Look for things like floating point math (keep it integer until the last possible operation), loops that don't quit as soon as they know they can quit (ie: if you've got 50 values ranging from 1 to 10 and you're trying to find the lowest value then stop looking as soon as you encounter a 1; it is the lowest value, by definition), extra math that doesn't need to happen, ...

Look up refactoring and optimisation.
There are techniques you can use that are common to most programing languages.
But no shortcuts, it comes from experience and reading about techniques.

Gordon