gnome-devel-docs master - en to zh translation catalogue (61 entries)

en: The Quick Guide to Optimizing GNOME Programs
zh: 优化文档的快速指南

en: This is a brief introduction to optimization, both the hows and the whys. Details of individual tools and techniques are left for later articles, but a collection of hints and tricks is provided.
zh: 这是关于优化的一个简单介绍，以及如何做和为何这么做。每个工具和技术的细节留到未来的文档中，但是提供了一些提示和技巧。

en: What are we Optimizing?
zh: 什么需要优化？

en: When we optimize for GNOME the first thing to remember is this: we are not trying to make the program better, we are trying to make the person using the computer happier.
zh: 优化 GNOME 程序时需要记住的第一件事情是：我们在做的不是使程序更好，而是让人们更快乐地使用计算机。

en: Better programs make people happier, but there are some improvements that will make them a lot happier than others: Responsiveness, start-up time, easy to access commands and not having the computer go into swap the moment more than two programs are open.
zh: 更好的程序可以使人们更快乐，但是一些改进将使他们比别人更快乐：反应速度、启动时间、更容易找到命令以及在多个程序同时打开时不让计算机卡住。

en: Doing the Optimization
zh: 进行优化

en: The previous section omitted one important qualifier: To optimize something it has to be measurable. You can't measure happiness. However, you can measure start-up time so you can tell if you have improved it. Happiness will then, hopefully, follow.
zh: 上面的章节忽略了一个重要的指标：优化的东西必须是可量测的。快乐是无法量测的，但是可以量测启动时间，然后讨论是否需要对其进行优化。希望快乐也能随之而来吧。

en: Optimization is the process of measurement, refinement and re-measurement. So the first thing you must do is find a way to measure what you are optimizing. Ideally this measurement is a single number, for example: the time taken to perform a task. This is your benchmark, it is the only way to tell if you are winning or losing. There is a big difference between a program that <emphasis>should</emphasis> be fast and a program that <emphasis>is</emphasis> fast.
zh: 优化是测量、精益求精和重新测量的过程。所以，第一件必须要做的事情就是找到一种测量要优化的事情的方法。这个测量结果最好是一个数字，例如：执行一个任务所需要的时间。这是您的性能测试，这时唯一可以告诉您通过还是失败的方法。在一个程序 <emphasis>应该</emphasis> 快还是 <emphasis>已经</emphasis> 快之间的区别。

en: Once you have a basic benchmark you need to find out why your code is not doing as well as it should. It is tempting to do this by inspection: just looking at the code and trying to spot something that looks like it needs improvement. You will invariably be wrong. Using a profiler to get a detailed break-down of what your program really does is the only way to be sure.
zh: 进行了基本的性能测试之后，您需要找出为什么代码没有如其应该的那样快。可以通过这样来尝试：检查代码找出需要改进的地方。您可能总会是错误的。使用一个性能分析器来获取详细的程序每步的工作是唯一可以保证是正确的方法。

en: Usually the problem is isolated to small sections of code. Pick the worst place and concentrate on that first. Once that is done, rerun the profiler and repeat. As you proceed the gains made at each step will get less and less, at some point you will have to decide that the results are good enough. If your efforts are only extracting 10% improvements then you are well past the point where you should have stopped.
zh: 程序通常可以分为很小的代码块。找出性能最差的地方并集中精力首先处理。完成之后，重新运行性能分析器然后重复。每进行一次这样的努力将获得的越来越少，您可以在一个点上认为结果已经足够好了。如果您的努力只能获取 10% 的改进，最好略过这个点，并且应该停止。

en: Don't forget the big picture. For example, rather than just trying to speed up a piece of code, ask yourself if it needs to be run at all. Could it be combined with another piece of code? Can the results of previous calculations be saved and reused? It won't even need to be optimized if it is in a place where the user is never going to notice it. Worse still, the code may already be optimized and is doing the heavy calculations now to avoid doing them later. Code does not run in isolation and neither does the optimization process.
zh: 不要忘记大图片。例如，在尝试加速一段代码之前，先想想是否真的需要它。是否可以和其它代码块结合？前面计算的结果是否可以保存起来并重用？如果是用户根本不会注意的地方就甚至不需要优化。更糟的是，代码已经优化了，并且正在进行高负载运算，那就需要避免稍候再次进行运算。不孤立运行的代码也不需要优化过程。

en: Hints
zh: 提示

en: The Fundamentals
zh: 基本原理

en: Re-run your benchmark after every change you make to the code and keep a log of everything you change and how it affects the benchmark. This lets you undo mistakes and also helps you not to repeat mistakes.
zh: 对代码进行更改之后重新运行性能测试，记录每次的更改及对性能的影响。这可以帮助您修复错误，也可以帮您避免重复犯错。

en: Make sure your code is correct and bug-free before optimizing it. Check that it remains correct and bug-free after optimization.
zh: 在进行优化之前确保代码是正确且没有缺陷的。在优化之后检查是否仍然是正确且没有缺陷的。

en: Optimize at the high level before optimizing the details.
zh: 在对细节进行优化之前先在高层进行优化。

en: Use the right algorithm. The classic text-book example is using quick-sort instead of bubble-sort. There are many others, some save memory, some save CPU. Also, see what shortcuts you can make: you can do quicker than quick-sort if you are prepared to make some compromises.
zh: 使用正确的算法。经典的文本排序使用的是快速排序而不是冒泡排序。还有很多其它，一些可以节省内存，一些可以节省 CPU。同样，寻找您可以走的捷径：如果愿意进行某些组合，甚至可以比快速排序更快。

en: Optimization is a trade-off. Caching results speeds up calculations, but increases memory use. Saving data to disk saves memory, but costs time when it is loaded back from disk.
zh: 优化就是折衷。缓存结果可以加速运行，但是会增加内存使用。将数据保存到磁盘可以节省内存，但从磁盘中加载时浪费时间。

en: Avoid expensive operations: Multiple small disk reads. Using up lots of memory so disk swapping becomes necessary. Avoid anything that writes or reads from the hard disk unnecessarily. The network is slow too. Also avoid graphics operations that need a response from the X server.
zh: 避免高消耗的操作：多次从磁盘读取很少的数据。使用很多内存会需要交换。避免任何不需要的硬盘读写。网络也是很慢的。同样避免需要 X 服务器响应的图形操作。

en: Traps for the Unwary
zh: 粗心造成的陷阱

en: Beware of side effects. There can often be strange interactions between different sections of code, a speed-up in one part can slow another part down.
zh: 注意副作用。在不同的代码部分之间经常有奇怪的交互，加速一个部分可能会使另一部分的速度降低。

en: When timing code, even on a quiet system, events outside the program add noise to the timing results. Average over multiple runs. If the code is very short, timer resolution is also a problem. In this case measure the time the computer takes to run the code 100 or 1000 times. If the times you are recording are longer than a few seconds, you should be OK.
zh: 测量代码的运行时间时，即使在安静的系统上，程序之外的事件也会对计时产生影响。多次运行取平均值。如果代码非常短，计时精度也是一个问题。这时，测量代码运行 100 甚至 1000 次的时间。如果您记录的时间长达数秒，就可以了。

en: It is very easy to be misled by the profiler. There are stories of people optimizing the operating system idle-loop because that is where it spent all its time! Don't optimize code that does nothing the user cares about.
zh: 也很容易被性能分析器误导。有人曾经优化掉了操作系统的 idle-loop，因为它耗费掉了系统所有的时间。不要优化做用户根本不关心的部分。

en: Remember the resources on the X server. Your program's memory usage doesn't include the pixmaps that are stored in the X server's process, but they are still using up memory. Use xrestop to see what resources your program is using.
zh: 别忘记了 X 服务器上的时间。您的程序的内存使用量并没有包含在 X 服务器进程中存储的像素影射，但它们仍然使用内存。使用 xrestop 查看您的程序使用的资源。

en: Low Level Hints
zh: 底层提示

en: When optimizing memory use, be wary of the difference between peak usage and average memory usage. Some memory is almost always allocated, this is usually bad. Some is only briefly allocated, this may be quite acceptable. Tools like massif use the concept of space-time, the product of memory used and the duration it was allocated for, instead.
zh: 优化内存使用时，警惕高峰使用量和平均使用量的区别。总是持有申请的内存是不好的。一些仅仅是简单的申请，还是可以接受的。类似于 massif 的工具使用时空 (内存使用量和使用时间的乘积) 的概念来代替。

en: Give the compiler as many hints as possible. Use the const keyword. Use <envar>G_INLINE_FUNC</envar> for short, frequently called, functions. Look up <envar>G_GNUC_PURE</envar>, <envar>G_LIKELY</envar> and the other glib miscellaneous macros. Use the macros instead of gcc-specific keywords to ensure portability.
zh: 给编译器尽可能多的提示。使用常量关键字。使用 <envar>G_INLINE_FUNC</envar> 作为经常调用的函数的简称。查看 <envar>G_GNUC_PURE</envar>，<envar>G_LIKELY</envar> 和其它 glib 杂项宏。使用这些宏代替 gcc 专用的关键字，以确保可移植性。

en: Don't use assembly language. It is not portable and, while it may be fast on one processor, it is not even guaranteed to be fast on every processor that supports that architecture (e.g. Athlon vs. Pentium 4).
zh: 不要使用汇编语言。汇编语言不可移植，而且虽然可能在一个处理器上很快，但是甚至在支持同样处理器架构的处理器上也不能保证可以很快 (例如速龙和奔四的区别)。

en: Don't rewrite an existing library routine unless you are sure it is unnecessarily slow. Many CPU-intensive library routines have already been optimized. Conversely, some library routines are slow, especially ones that make system calls to the operating system.
zh: 不要重写已有的库函数，除非确定库函数不可接受得慢。很多 CPU 敏感的库函数都是优化过的。相反地，一些库函数也是慢的，尤其是那些使用系统调用的。

en: Minimize the number of libraries you link to. The fewer libraries to link in, the faster the program starts. This is a difficult thing to do with GNOME.
zh: 使用尽量少的库。链接到的库越少，程序启动越快。对于 GNOME，这很难达到。

en: High Level Tricks
zh: 高层技巧

en: Take advantage of concurrency. This doesn't just mean using multiple processors, it also means taking advantage of the time the user spends thinking about what they are going to do next to perform some calculations in anticipation. Do calculations while waiting for data to be loaded off disk. Take advantage of multiple resources, use them all at once.
zh: 发挥并发的优势。这并不仅仅意味着使用多处理器，也意味着减少用户执行一些计算所预期的时间。在等待从磁盘上加载数据是进行计算。充分使用多种资源，同时使用它们。

en: Cheat. The user only has to think that the computer is fast, it doesn't matter whether it actually is or not. It is the time between the command and the answer that is important, it doesn't matter if the response is pre-calculated, cached, or will in fact be worked out later at a more convenient time, as long as the user gets what they expect.
zh: 欺骗。用户仅仅认为计算机很快，而不管实际上是否如此。命令和结果之间的时间很重要，而无论是提前计算的、缓存的还是在稍候方便的时间进行实际计算，尽快地给出用户期望的结果。

en: Do things in the idle loop. It is easier to program than using full multi-threading but still gets things done out of the users eye. Be careful though, if you spend too long in the idle loop your program will become sluggish. So regularly give control back to the main loop.
zh: 在空闲循环中进行某些工作。这比多线程更容易编程但仍然在用户视线之外进行了工作。仍然需要注意，如果在空闲循环中花费太多时间，程序将变卡。所以需要经常返回主循环。

en: If all else fails, tell the user that the code is going to be slow and put up a progress bar. They won't be as happy as if you had just presented the results, but they will at least know the program hasn't crashed and they can go get a cup of coffee.
zh: 如果都失败了，告诉用户代码将会比较慢并且使用进度条。如果仅仅给出结果，用户并不会高兴，他们至少会知道程序没有崩溃，他们可以去喝杯咖啡。

en: Using <application>Massif</application> for Profiling Memory Use in GNOME Software
zh: 使用 <application>Massif</application> 分析 GNOME 软件的内存使用

en: This article describes how to use the <application>Massif</application> heap profiler with GNOME applications. We describe how to invoke, interpret, and act on the output of <application>Massif</application>. The <application>Same GNOME</application> game is used as an example.
zh: 这篇文章描述怎样在 GNOME 程序中使用 <application>Massif</application> 堆分析器，解释怎样调用、理解已经对 <application>Massif</application> 的输出进行操作。

en: Introduction
zh: 简介

en: <application>Massif</application> is a member of the <ulink type="http" url="http://valgrind.org/">valgrind</ulink> suite of memory-profiling tools. Its purpose is to give a detailed view of dynamic memory usage during the lifetime of the program. Specifically it records the memory use of the heap and the stack.
zh: <application>Massif</application> 是 <ulink type="http" url="http://valgrind.org/">valgrind</ulink> 内存分析套件的一个成员。它的目的是在程序的生命期里给出动态内存使用的细节。它会专门记录堆栈的内存使用。

en: The heap is the region of memory which is allocated with functions like malloc. It grows on demand and is usually the largest region of memory in a program. The stack is where all the local data for functions is stored. This includes the "automatic" variables in C and the return address for subroutines. The stack is typically a lot smaller and a lot more active than the heap. We won't consider the stack explicitly since <application>Massif</application> treats it as though it were just another part of the heap. <application>Massif</application> also gives information about how much memory is used to manage the heap.
zh: 堆是使用类似于 malloc 的函数分配内存的地方。它根据需要增长，而且通常是程序中最大的内存区域。栈是存放函数的本地数据的地方，包括 C 语言中的自动变量和子函数的返回地址。栈通常比堆更小更活跃。我们并没有显式地考虑栈，因为 <application>Massif</application> 将其认定为堆的另外一部分。<application>Massif</application> 也给出管理堆所使用的内存量。

en: <application>Massif</application> produces two output files: a graphical overview in a postscript file and a detailed breakdown in a text file.
zh: <application>Massif</application> 产生两个输出文件：一个 postscript 格式的概况以及一个文本格式的细节详述。

en: Using <application>Massif</application> with GNOME
zh: 在 GNOME 中使用 <application>Massif</application>

en: Your command-line should therefore look something like:
zh: 因此，您的命令行应该类似于：

en: <application>Same GNOME</application> is the program we will be using as an example. Be warned that, since valgrind emulates the CPU, it will run <emphasis>very</emphasis> slowly. You will also need a lot of memory.
zh: <application>Same GNOME</application> 是我们用来作为范例的程序。必须要警告：因为 valgrind 仿真 CPU，它运行得将会<emphasis>非常</emphasis> 慢，同时也需要很多内存。

en: Interpreting the Results
zh: 对结果的解释

en: <application>Massif</application> output for the unoptimized version of the <application>Same GNOME</application> program.
zh: 未优化版本的 <application>Same GNOME</application> 程序的 <application>Massif</application> 输出。

en: <application>Massif</application> output for the optimized <application>Same GNOME</application> program.
zh: 优化后的 <application>Same GNOME</application> 程序的 <application>Massif</application> 输出。

en: Can we do better ? A quick examination of <application>Massif</application>'s text output reveals: g_strdup to be the new major offender.
zh: 我们可以做得更好吗？快速检查 <application>Massif</application> 的文本输出揭示：g_strdup 是新的主犯。

en: If we look closer though we see that it is called from many, many, places.
zh: 如果我们更自习的查看，我们发现它从很多很多地方调用。

en: Caveats
zh: 注意事项

en: Disk seeks are one of the most expensive operations you can possibly perform. You might not know this from looking at how many of them we perform, but trust me, they are. Consequently, please refrain from the following suboptimal behavior:
zh: 磁盘寻道是您可能遇到的最昂贵的操作之一。从查看执行的数量，可能不能了解到，但这是真的。相应地，请避免如下次优的选择：

en: Placing lots of small files all over the disk.
zh: 在磁盘中到处存放很多小文件。

en: Opening, stating, and reading lots of files all over the disk
zh: 在磁盘中，到处打开、查看、读取很多小文件。

en: Keep data together in the same directory.
zh: 将数据保持在同一个目录中。

en: Cache data so as to not need to reread constantly.
zh: 缓存数据以避免定期重读数据。

en: Share data so as not to have to reread it from disk when each application loads.
zh: 共享数据以便不必每个程序需要时都从硬盘读取。

en: Consider caching all of the data in a single binary file that is properly aligned and can be mmaped.
zh: 考虑将所有的数据缓存在单个二进制文件中，并使文件正确对齐并可以将其 mmap。

en: Optimizing GNOME Software
zh: 优化 GNOME 软件

en: GNOME Documentation Project
zh: GNOME 文档项目

en: November 2007
zh: 2007年11月

en: Intial conversion to docbook format.
zh: 初始转换到 docbook 格式。
