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.
  • #1
jack476
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
Technology news on Phys.org
  • #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
harborsparrow said:
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 to A mathematical way to analyze code?

1. What is "A mathematical way to analyze code"?

"A mathematical way to analyze code" refers to the use of mathematical concepts and techniques to evaluate and improve the efficiency, correctness, and complexity of computer programs.

2. Why is mathematical analysis important for coding?

Mathematical analysis allows programmers to identify and eliminate inefficiencies and errors in their code, leading to more optimized and reliable programs.

3. What are some common mathematical techniques used for code analysis?

Some common mathematical techniques used for code analysis include algorithms, data structures, complexity analysis, and formal methods.

4. How can mathematical analysis benefit software development?

Mathematical analysis can improve software development by helping programmers write more efficient and reliable code, reducing development time and cost, and ensuring higher quality products.

5. Is mathematical knowledge necessary for code analysis?

While a strong understanding of mathematics can greatly aid in code analysis, it is not necessary. Many tools and frameworks exist that can perform mathematical analysis on code without requiring extensive mathematical knowledge from the programmer.

Similar threads

  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
29
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
11
Views
611
  • Programming and Computer Science
Replies
19
Views
2K
Replies
16
Views
2K
  • STEM Academic Advising
Replies
21
Views
2K
  • Science and Math Textbooks
Replies
13
Views
3K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top