A mathematical way to analyze code?

  • Thread starter Thread starter jack476
  • Start date Start date
  • Tags Tags
    Code Mathematical
Click For Summary
The discussion centers on optimizing code for microcontroller programming, highlighting the potential for mathematical techniques to enhance code efficiency, speed, and energy consumption. It notes that while compilers perform some optimizations, achieving optimal code often involves trade-offs between efficiency, maintainability, and clarity. The conversation references Donald Knuth's work on algorithm performance and the challenges of inventing new algorithms. It emphasizes that optimization can focus on various metrics, such as execution cycles and memory usage, but cannot address all aspects simultaneously. Automated tools in modern programming environments can assist in code analysis, although advanced optimization tools may require paid software. The use of lambda calculus for proving program correctness is also mentioned, indicating its relevance in ensuring programs terminate correctly, though it may not align perfectly with contemporary programming practices. The discussion concludes with a personal reflection on the speaker's background in computer science and engineering, suggesting a foundation for further exploration of these concepts.
jack476
Messages
327
Reaction score
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
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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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