A mathematical way to analyze code?

  • Thread starter Thread starter jack476
  • Start date Start date
  • Tags Tags
    Code Mathematical
Click For Summary

Discussion Overview

The discussion revolves around the potential application of mathematical techniques to optimize code in programming, particularly in the context of microcontroller programming. Participants explore various aspects of code optimization, including performance, efficiency, and the theoretical underpinnings of algorithm design.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant suggests using mathematical techniques to optimize code similarly to how Kirchhoff's laws simplify circuit analysis.
  • Another mentions that some compilers perform optimizations on machine language code, referencing a Wikipedia article on program optimization techniques.
  • A participant highlights the challenge of inventing algorithms for algorithm design, referencing Donald Knuth's work on algorithm performance.
  • There is a discussion about the various factors that influence optimization, such as total cycles, memory usage, and CPU architecture.
  • One participant notes the trade-off between code efficiency and maintainability, stating that optimizing for one aspect may negatively impact another.
  • Automated tools in modern programming environments can help analyze code for efficiency, but access to advanced optimization tools may require purchasing software.
  • Lambda calculus is mentioned as a method for proving program correctness, with a focus on specifying pre-conditions and post-conditions for loops.
  • A participant expresses uncertainty about the relevance of lambda calculus to modern programming paradigms and shares their background in computer science and mathematics.

Areas of Agreement / Disagreement

Participants express a range of views on the role of mathematical techniques in code optimization, with no clear consensus on the effectiveness or applicability of these methods. The discussion remains unresolved regarding the best approaches to optimization and the relevance of lambda calculus.

Contextual Notes

Participants acknowledge the complexity of defining optimization and the various factors that must be considered, such as the specific programming context and the trade-offs involved in different optimization strategies.

jack476
Messages
327
Reaction score
124
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   Reactions: Silicon Waffle
Technology news on Phys.org
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
 
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)
 
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.
 
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 :-).
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
29
Views
6K
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
16
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
60
Views
18K
  • · Replies 21 ·
Replies
21
Views
5K