Home php教程 php手册 代码优化的另一面

代码优化的另一面

Jun 21, 2016 am 08:50 AM
linux nbsp

 

优化软件是一件好事,但如果使用不当,就会好事变坏事。如果你在优化代码时走向了错误的道路,那么这种优化会提高开发成本、降低生产力。在软件开发过程中,成本需要时刻谨记在心。一般来说,优化的软件需要花费更长的时间来交付,因为你需要花费精力使它质量更高。有时候,你并不是为了运行速度而做优化。对于嵌入式系统来说,可能是减少内存使用,对于手持设备,可能是硬件资源限制。优化的代码通常难以调试和维护,因为你需要牺牲一些代码可读性。优化良好的软件带来的好处要多于坏处,但是如果你做了错误的优化,那么结果恰恰相反。

到底怎么才能做好代码优化呢?Rick Cook给出了一些有用的建议。

你到底为了什么而优化

如果在优化过程启动时搞不清楚为什么而优化,那么你基本会走向错误的道路。你需要清楚的理解你准备完成的目标和相关的优化选择。这些目标需要清晰而且简洁,简单到项目经理能够理解它,你需要在优化过程中始终坚持这些目标。在软件开发过程中,变更是常有的事情。你可能一开始想优化这个目标,然后又发现需要优化其他目标。事实也是如此,但是请把这些目标的变更记录清楚。

性能测试团队报告的性能缺陷可能是优化的主要目标,一方面它来自于开发人员之外的客观性能问题,另一方面这些缺陷报告会明确的指出存在哪些问题,是运行缓慢,还是磁盘换页频繁等等。开发人员从这些缺陷中开始入手优化,比自己空想出的性能目标要合理、客观的多。

小心对待优化的衡量标准

选择正确的衡量标准是优化的重要步骤。你需要利用这些标准来衡量自己的优化进度。如果衡量标准是错误的,那么你的努力就白费了。即使是正确的标准,也需要正确的运用。有时候,把主要精力放在应用程序运行时间最多的代码部分上市正确的做法。不过请记住,Unix/Linux内核在空闲循环(idle loop)中花费的时间最多。这里的问题是,如果你不小心对待,那么你可能会选择一个不能帮助你解决问题的衡量标准。

衡量标准的选择应该取决于哪些标准能够确实提高用户体验。例如,有关数据库的性能分析指标有很多,开发人员和性能测试人员需要确定哪些指标是真正影响应用程序速度的,是bufferpool的大小,还是数据库连接池的大小。这是一个渐进的认识过程。

优化且只优化关键部分

这是有效优化的关键。寻找能够达到目标(性能、资源)的代码部分并集中精力。一个典型的例子是把时间花费在优化数据库上,而实际的性能杀手是缓慢的网络连接。

不要被映入眼帘的表象所吸引。这些表象并不一定会解决你的问题。只是因为某些事情易于发现而且易于优化并不意味着它们值得关注。 

高层次优化更好

一般来说,优化的层次越高,优化的效果就越明显。按此标准,最好的优化方法是寻找更好的算法。例如,在一些IT部门,员工花费几个月的时间来对某个软件做优化但是没有进展,然后找来一批新员工来做这些优化,他们很快就会发现性能的问题在于代码某处使用了冒泡排序或者某张数据库表增加了数以万条的记录等等。

建议大家花时间看看基本的应用架构,找找有没有可以优化的线索。但是,高层次优化不是银弹。一些基本的技术,比如把代码尽可能的移到循环体外面等等。

另外,高层次优化可以避免对代码细节的复杂重构。开发人员往往对低层次的优化最感兴趣,请控制住自己的欲望,从高处着手! 

不要过早优化

开发人员有一种冲动,那就是在编码的时候就准备优化了。一般来说,这不是个好主意。有时候,开发人员并不确定这样的优化工作是否值得。例如,你可能通过移位操作来代替乘法操作,但是这种性能优化的做法会产生让人非常难以理解的代码。

最好把开发和优化工作分开,先开发出正确的代码,然后再优化。过早优化的问题在于开发人员会有意的对软件的架构设计和代码结构等做一些预先的设想,而其中有相当一部分都是多余操心的,你可能不得不对这些多余的部分再做优化。

依靠性能分析数据,而不是直觉

你以为自己知道软件系统哪里需要优化,但是直觉是第二位的,数据是第一位的。否则,你会发现可能把一段代码优化的非常快,但是实际上很少被调用。

优化的一个有效的策略是,你要根据所做工作对优化效果的影响来进行排序。在开始工作之前找到影响最大的“路障”,然后再处理小的“路障”。 

不能指望优化解决所有问题

优化的重要法则之一是不能让优化解决所有问题,比如,提高运行速度会耗费更多系统资源。你必须为了主要的优化目标做出权衡。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles