- Mastering Delphi Programming:A Complete Reference Guide
- Primo? Gabrijel?i?
- 381字
- 2021-06-24 12:33:31
Fixing the Algorithm
In the previous chapter, we explored the concept of performance and looked at different scenarios where we would like to make the program faster. The previous chapter was largely theoretical, but now is the time to look at it in a more practical way.
There are two main approaches to speeding up a program:
- Replace the algorithm with a better one.
- Fine-tune the code so that it runs faster.
I spent lots of time in the previous chapter discussing the time complexity simply to make it clear that a difference between two algorithms can result in impressive speed-up. It can be much more than a simple constant factor (such as a 10-times speed-up). If we go from an algorithm with bad time complexity (say, O(n2)) to an algorithm with a better behavior (O(n log n), for example), then the difference in speed becomes more and more noticeable when we increase the size of the data.
Saying all that, it should not be surprising that I prefer the first approach (fixing the algorithm) to the second one (fine-tuning the code). To continue this point, this chapter will deal with several practical examples of speeding up the code by changing the algorithm.
Firstly, we will look at user interfaces. As we can't speed up VCL or Windows by fine-tuning the code (simply because we cannot modify that code), any speed-up in user interface responsiveness can only be a result of a better algorithm.
After that, I'll introduce the concept of caching. This approach can help you when everything else fails. Maybe it is not possible to change an algorithm, but introducing a cache of frequently-calculated values can still improve the code speed drastically.
As implementing a fast cache is quite a tricky business, I'll also present a generic cache class that you can use freely in your own code.
Before concluding the chapter, I'll look at Mr. Smith's SlowCode example and try to speed it up by implementing a better algorithm.
We will cover the following topics in this chapter:
- Writing responsive user interfaces
- How to update VCL and FMX controls without waiting
- Speeding up functions by introducing caching
- How to implement a reusable generic caching class
- How to analyze and improve an unknown algorithm
- 新媒體跨界交互設計
- 觸摸屏實用技術與工程應用
- 深入理解Spring Cloud與實戰
- 顯卡維修知識精解
- Learning AngularJS Animations
- Mastering Adobe Photoshop Elements
- CC2530單片機技術與應用
- STM32嵌入式技術應用開發全案例實踐
- Machine Learning with Go Quick Start Guide
- 單片機原理及應用:基于C51+Proteus仿真
- Hands-On Deep Learning for Images with TensorFlow
- USB應用分析精粹:從設備硬件、固件到主機端程序設計
- 計算機組裝與維護(慕課版)
- 快·易·通:2天學會電腦組裝·系統安裝·日常維護與故障排除
- 3D打印:Geomagic Design X5.1 逆向建模設計實用教程