gem-graph-client/doc/GTK-docs/gnome-dev-documentation/developer.gnome.org/documentation/guidelines/programming/optimizing.html

467 lines
31 KiB
HTML
Raw Normal View History

<!doctype html>
<html class="no-js">
<head><meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="color-scheme" content="light dark"><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<link rel="index" title="Index" href="../../genindex.html" /><link rel="search" title="Search" href="../../search.html" /><link rel="next" title="Namespacing" href="namespacing.html" /><link rel="prev" title="The Importance of Writing Good Code" href="writing-good-code.html" />
<meta name="generator" content="sphinx-4.3.0, furo 2022.06.21"/>
<title>Optimizing GNOME Applications - GNOME Developer Documentation</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo.css?digest=40978830699223671f4072448e654b5958f38b89" />
<link rel="stylesheet" type="text/css" href="../../_static/tabs.css" />
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
<link rel="stylesheet" type="text/css" href="../../_static/gnome.css" />
<style>
body {
--color-code-background: #f8f8f8;
--color-code-foreground: black;
--color-brand-primary: #4a86cf;
--color-brand-content: #4a86cf;
}
@media not print {
body[data-theme="dark"] {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
@media (prefers-color-scheme: dark) {
body:not([data-theme="light"]) {
--color-code-background: #202020;
--color-code-foreground: #d0d0d0;
}
}
}
</style></head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="svg-toc" viewBox="0 0 24 24">
<title>Contents</title>
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
<path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
</svg>
</symbol>
<symbol id="svg-menu" viewBox="0 0 24 24">
<title>Menu</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
<line x1="3" y1="12" x2="21" y2="12"></line>
<line x1="3" y1="6" x2="21" y2="6"></line>
<line x1="3" y1="18" x2="21" y2="18"></line>
</svg>
</symbol>
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
<title>Expand</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</symbol>
<symbol id="svg-sun" viewBox="0 0 24 24">
<title>Light mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
</symbol>
<symbol id="svg-moon" viewBox="0 0 24 24">
<title>Dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
</svg>
</symbol>
<symbol id="svg-sun-half" viewBox="0 0 24 24">
<title>Auto light/dark mode</title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-shadow">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<circle cx="12" cy="12" r="9" />
<path d="M13 12h5" />
<path d="M13 15h4" />
<path d="M13 18h1" />
<path d="M13 9h4" />
<path d="M13 6h1" />
</svg>
</symbol>
</svg>
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation">
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc">
<label class="overlay sidebar-overlay" for="__navigation"></label>
<label class="overlay toc-overlay" for="__toc"></label>
<div class="page">
<header class="mobile-header">
<div class="header-left">
<label class="nav-overlay-icon" for="__navigation">
<i class="icon"><svg><use href="#svg-menu"></use></svg></i>
</label>
</div>
<div class="header-center">
<a href="../../index.html"><div class="brand">GNOME Developer Documentation</div></a>
</div>
<div class="header-right">
<label class="toc-overlay-icon toc-header-icon" for="__toc">
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
</div>
</header>
<aside class="sidebar-drawer">
<div class="sidebar-container">
<div class="sidebar-sticky"><a class="sidebar-brand" href="../../index.html">
<span class="sidebar-brand-text">GNOME Developer Documentation</span>
</a><form class="sidebar-search-container" method="get" action="../../search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
<input type="hidden" name="check_keywords" value="yes">
<input type="hidden" name="area" value="default">
</form>
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1 has-children"><a class="reference internal" href="../../introduction.html">Platform Introduction</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../../introduction/components.html">Platform Components</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../../introduction/overview/libraries.html">Libraries</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../introduction/overview/services.html">Services</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../introduction/languages.html">Programming Languages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../introduction/builder.html">GNOME Builder</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../introduction/flatpak.html">Flatpak</a></li>
</ul>
</li>
<li class="toctree-l1 current has-children"><a class="reference internal" href="../../guidelines.html">Guidelines</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current">
<li class="toctree-l2 current has-children"><a class="reference internal" href="../programming.html">Programming Guidelines</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="coding-style.html">C Coding Style</a></li>
<li class="toctree-l3"><a class="reference internal" href="memory-management.html">Managing Memory</a></li>
<li class="toctree-l3"><a class="reference internal" href="writing-good-code.html">The Importance of Writing Good Code</a></li>
<li class="toctree-l3 current current-page"><a class="current reference internal" href="#">Optimizing GNOME Applications</a></li>
<li class="toctree-l3"><a class="reference internal" href="namespacing.html">Namespacing</a></li>
<li class="toctree-l3"><a class="reference internal" href="introspection.html">Introspection</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../accessibility.html">Accessibility</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../accessibility/coding-guidelines.html">Coding Guidelines for Supporting Accessibility</a></li>
<li class="toctree-l3"><a class="reference internal" href="../accessibility/custom-widgets.html">Making Custom Components Accessible</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../localization.html">Localization</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../localization/practices.html">Best Practices for Localization</a></li>
</ul>
</li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../maintainer.html">Maintainer Guidelines</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3"><a class="reference internal" href="../maintainer/api-stability.html">API Stability</a></li>
<li class="toctree-l3"><a class="reference internal" href="../maintainer/parallel-installability.html">Parallel Installability</a></li>
<li class="toctree-l3"><a class="reference internal" href="../maintainer/integrating.html">Integrating with GNOME</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../devel-docs.html">Developer Documentation Style Guidelines</a></li>
</ul>
</li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../../tutorials.html">Tutorials</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../../tutorials/beginners.html">Beginners Tutorials</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l3 has-children"><a class="reference internal" href="../../tutorials/beginners/getting_started.html">Getting Started</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/content_view.html">Adding A Content View</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/opening_files.html">Loading Content From A File</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/cursor_position.html">Showing The Cursor Position</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/saving_files.html">Saving The Content To A File</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/saving_state.html">Saving The Application State</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/adding_toasts.html">Notifying The User With Toasts</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/dark_mode.html">Forcing The Dark Color Scheme</a></li>
</ul>
</li>
<li class="toctree-l3 has-children"><a class="reference internal" href="../../tutorials/beginners/components.html">UI components</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/window.html">Windows</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/image.html">Images</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/label.html">Labels</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/button.html">Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/box.html">Boxes</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/stack.html">Stacks</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/leaflet.html">Leaflets</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/toggle.html">Toggles</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/check_box.html">Check Boxes</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/radio_button.html">Radio Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/spin_button.html">Spin Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/link_button.html">Link Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/switch.html">Switches</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/menu_button.html">Menu Buttons</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/entry.html">Entries</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/password_entry.html">Password Entries</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/message_dialog.html">Messages</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/file_dialog.html">File Dialogs</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/spinner.html">Spinners</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/level_bar.html">Level Bars</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/application-id.html">Application ID</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/application.html">Using GtkApplication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/save-state.html">Saving and Loading Window State</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/notifications.html">Using Notifications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/themed-icons.html">Themed Icons</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/deprecations.html">Dealing With Deprecations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/actions.html">Actions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/menus.html">Menus</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/search-provider.html">Writing a Search Provider</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/pre-and-post-conditions.html">Pre- and Post-Conditions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/main-contexts.html">Main Contexts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/lists.html">Using GLib Lists</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/threading.html">Threading</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/asynchronous-programming.html">Asynchronous Programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/drag-and-drop.html">Drag and Drop</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/widget-templates.html">Widget Templates</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</aside>
<main class="main">
<div class="content">
<article role="main">
<label class="toc-overlay-icon toc-content-icon" for="__toc">
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
</label>
<section id="optimizing-gnome-applications">
<h1>Optimizing GNOME Applications<a class="headerlink" href="#optimizing-gnome-applications" title="Permalink to this headline">#</a></h1>
<section id="what-are-we-optimizing">
<h2>What are we optimizing?<a class="headerlink" href="#what-are-we-optimizing" title="Permalink to this headline">#</a></h2>
<p>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.</p>
<p>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.</p>
<p>Traditional optimization tackles concepts like CPU use, code size, the number of
mouse clicks and the memory use of the program. This second list has been chosen
to correlate with the first list, however there is an important difference: the
person using GNOME doesnt care about the second list, but they care a lot about
the first list. When optimizing GNOME programs we will reduce CPU use, memory
use and all those things, but these are the means to the end, not the final
goal. We are optimizing for people.</p>
</section>
<section id="optimization">
<h2>Optimization<a class="headerlink" href="#optimization" title="Permalink to this headline">#</a></h2>
<p>The previous section omitted one important qualifier: to optimize something it
has to be measurable. You cant measure happiness. However, you can measure
start-up time so you can tell if you have improved it. Happiness will then,
hopefully, follow.</p>
<p>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 should be
fast and a program that is fast.</p>
<p>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.</p>
<p>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.</p>
<p>Dont 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 wont 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.</p>
</section>
<section id="hints">
<h2>Hints<a class="headerlink" href="#hints" title="Permalink to this headline">#</a></h2>
<section id="the-fundamentals">
<h3>The fundamentals<a class="headerlink" href="#the-fundamentals" title="Permalink to this headline">#</a></h3>
<p>1. 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.</p>
<p>2. Make sure your code is correct and bug-free before optimizing it. Check that
it remains correct and bug-free after optimization.</p>
<ol class="arabic simple" start="3">
<li><p>Optimize at the high level before optimizing the details.</p></li>
</ol>
<p>4. 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.</p>
<p>5. 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.</p>
<p>6. Make sure you choose a wide variety of inputs to optimize against. If you
dont it is easy to end up with a piece of code carefully optimized for one file
and no others.</p>
<p>7. 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.</p>
</section>
<section id="traps-for-the-unwary">
<h3>Traps for the unwary<a class="headerlink" href="#traps-for-the-unwary" title="Permalink to this headline">#</a></h3>
<p>1. 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.</p>
<p>2. 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.</p>
<p>3. 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! Dont optimize code that does nothing the user cares about.</p>
<p>4. Remember the resources on the X server. Your programs memory usage doesnt
include the pixmaps that are stored in the X servers process, but they are
still using up memory. Use xrestop to see what resources your program is using.</p>
</section>
<section id="low-level-hints">
<h3>Low level hints<a class="headerlink" href="#low-level-hints" title="Permalink to this headline">#</a></h3>
<p>1. 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.</p>
<p>2. Time simplified bits of code that do only the things you know are essential,
this gives an absolute lower limit on the time your code will take. For example,
when optimizing a loop time the empty loop. If that is still too long no amount
of micro-optimization will help and you will have to change your design. Make
sure the compiler doesnt optimize away your empty loop.</p>
<p>3. Move code out from inside loops. A slightly more complicated piece of code
that is executed once is far quicker than a simple piece of code executed a
thousand times. Avoid calling slow code often.</p>
<p>4. Give the compiler as many hints as possible. Use the const keyword. Use
<code class="docutils literal notranslate"><span class="pre">G_INLINE_FUNC</span></code> for short, frequently called, functions. Look up
<code class="docutils literal notranslate"><span class="pre">G_GNUC_PURE</span></code>, <code class="docutils literal notranslate"><span class="pre">G_LIKELY</span></code> and the other GLib miscellaneous macros. Use the
macros instead of compiler-specific keywords to ensure portability.</p>
<p>5. Dont 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.</p>
<p>6. Dont 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.</p>
<p>7. Minimize the number of libraries you link to. The fewer libraries to link in,
the faster the program starts. This can be a difficult thing to do with GNOME.</p>
</section>
<section id="high-level-tricks">
<h3>High level tricks<a class="headerlink" href="#high-level-tricks" title="Permalink to this headline">#</a></h3>
<p>1. Take advantage of concurrency. This doesnt 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.</p>
<p>2. Cheat. The user only has to think that the computer is fast, it doesnt
matter whether it actually is or not. It is the time between the command and the
answer that is important, it doesnt 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.</p>
<p>3. 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.</p>
<p>4. If all else fails, tell the user that the code is going to be slow and put up
a progress bar. They wont be as happy as if you had just presented the results,
but they will at least know the program hasnt crashed and they can go get a cup
of coffee.</p>
</section>
</section>
</section>
</article>
<footer>
<div class="related-pages">
<a class="next-page" href="namespacing.html">
<div class="page-info">
<div class="context">
<span>Next</span>
</div>
<div class="title">Namespacing</div>
</div>
<svg><use href="#svg-arrow-right"></use></svg>
</a>
<a class="prev-page" href="writing-good-code.html">
<svg><use href="#svg-arrow-right"></use></svg>
<div class="page-info">
<div class="context">
<span>Previous</span>
</div>
<div class="title">The Importance of Writing Good Code</div>
</div>
</a>
</div>
</footer>
</div>
<aside class="toc-drawer">
<div class="toc-sticky toc-scroll">
<div class="toc-title-container">
<span class="toc-title">
Contents
</span>
</div>
<div class="toc-tree-container">
<div class="toc-tree">
<ul>
<li><a class="reference internal" href="#">Optimizing GNOME Applications</a><ul>
<li><a class="reference internal" href="#what-are-we-optimizing">What are we optimizing?</a></li>
<li><a class="reference internal" href="#optimization">Optimization</a></li>
<li><a class="reference internal" href="#hints">Hints</a><ul>
<li><a class="reference internal" href="#the-fundamentals">The fundamentals</a></li>
<li><a class="reference internal" href="#traps-for-the-unwary">Traps for the unwary</a></li>
<li><a class="reference internal" href="#low-level-hints">Low level hints</a></li>
<li><a class="reference internal" href="#high-level-tricks">High level tricks</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</aside>
</main>
</div><script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script>
<script src="../../_static/underscore.js"></script>
<script src="../../_static/doctools.js"></script>
<script src="../../_static/scripts/furo.js"></script>
</body>
</html>