A mathematical way to analyze code?

  • Thread starter jack476
  • Start date
  • #1
324
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

Answers and Replies

  • #2
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,798
1,670
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
anorlunda
Staff Emeritus
Insights Author
10,429
7,652
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
chiro
Science Advisor
4,797
133
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
613
155
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
324
125
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.
 

Related Threads on A mathematical way to analyze code?

  • Last Post
Replies
0
Views
3K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
4
Views
793
  • Last Post
Replies
21
Views
3K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
4K
Replies
1
Views
4K
  • Last Post
Replies
7
Views
1K
Replies
5
Views
1K
Top