A mathematical way to analyze code?

In summary, mathematical techniques can be used to optimize code or find equivalent functions for the same task. There is a trade-off to be made between making the code easy to maintain and understand and making it efficient and fast.f
  • #1
328
125
In the last semester I was taking a microcontroller programming course. For the programming assignments, our professor would hold an extra credit competition where we competed for the smallest, fastest, and most energy-efficient code for the same application. It got me thinking.

What I'm wondering is if there is a way to use mathematical techniques to optimize code or find equivalent functions for the same task in the same way that we could, for instance, use Kirchoff's laws to find simpler equivalent circuits.
 
  • Like
Likes Silicon Waffle
  • #2
Some language compilers analyze the machine language code they generate and perform certain kinds of optimizations on the final code.

Here is an article which discusses broadly program optimization techniques:

http://en.wikipedia.org/wiki/Program_optimization
 
  • #3
Academics are fond of mathematical correctness proofs for software. Donald Knuth's books "The Art And Science of Computer Programming" go extensively into analyzing algorithm performance.

But algorithms to invent and innovate are much harder to do.

Why don't you invent an algorithm to invent algorithms to write algorithms? (Just kidding)
 
  • #4
You have to decide what optimization is in terms of what it seeks to do.

You can optimize over many things - like number of total cycles to run, number of bytes for your code, or even other things like using the architecture of your CPU in useful ways.

The instruction set, architecture, memory model, and a number of other things affect this.
 
  • #5
Computer scientists commonly say that making code efficient and fast is directly in conflict with making code easy to maintain and understand. So there is a trade-off to be made. Even making code small (to occupy little memory) could make it run less fast. You cannot optimize for everything at once.

In modern programming environments, there will often typically be a number of automated tools to help you analyze your code, finding code that is never called, for example, or tracing where the program spends most of its time, or citing instances of bad coding practices.

Microsoft makes the programming development environment available for free, but if you want to get the tools that help you optimize code, you'll have to pay for one of the more expensive versions of their IDE (Visual Studio .NET).

Lastly, in computer science, quite a study has been made of using lambda calculus to "prove" program correctness--to try and prove that a specific program will terminate and not hang, for example. Going into a loop, this discipline will require you to explicitly state all pre-conditions for the loop to operate correctly. Coming out of a loop, similarly, you'd be asked to specify all the things that should have resulted from the loop. There was an attempt or desire for some years to use this discipline of mathematically proving a program to be correct actually to generate code. I lost track of all this stuff years ago, but if you are interested in it, you would need to take college level courses in computer science. Or do a lot of precocious reading on your own :-).
 
  • #6
Lastly, in computer science, quite a study has been made of using lambda calculus to "prove" program correctness--to try and prove that a specific program will terminate and not hang, for example. Going into a loop, this discipline will require you to explicitly state all pre-conditions for the loop to operate correctly. Coming out of a loop, similarly, you'd be asked to specify all the things that should have resulted from the loop. There was an attempt or desire for some years to use this discipline of mathematically proving a program to be correct actually to generate code. I lost track of all this stuff years ago, but if you are interested in it, you would need to take college level courses in computer science. Or do a lot of precocious reading on your own :).

I did a fair bit of reading into Lambda calculus, actually, but from what I was finding it's not up to date with modern programming paradigms, or would that be incorrect? Because I feel like that would be very useful to know.

I'm not sure what my level of CS preparation would be. I've taken a few computer engineering courses in my electronics engineering program, so C, numerical methods, digital logic, micro-controllers, and computer architecture, and mathematically I've made it through ODE and I'll be taking a discrete math course this semester.
 

Suggested for: A mathematical way to analyze code?

2
Replies
60
Views
2K
Replies
22
Views
1K
Replies
12
Views
906
Replies
34
Views
2K
Replies
2
Views
577
Replies
3
Views
586
Replies
5
Views
653
Back
Top