pygame-vs-raylib.html (40255B)
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta charset="utf-8" /> 5 <meta name="generator" content="pandoc 3.10.2" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> 7 <title>pygame-vs-raylib</title> 8 <style> 9 /* Default styles provided by pandoc. 10 ** See https://pandoc.org/MANUAL.html#variables-for-html for config info. 11 */ 12 span.smallcaps{font-variant: small-caps;} 13 div.columns{display: flex; gap: 1.5em;} 14 div.column{flex: auto;} 15 @media screen { 16 div.columns{gap: min(4vw, 1.5em);} 17 div.column{overflow-x: auto;} 18 } 19 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} 20 /* The extra [class] is a hack that increases specificity enough to 21 override a similar rule in reveal.js */ 22 ul.task-list[class]{list-style: none;} 23 ul.task-list li input[type="checkbox"] { 24 font-size: inherit; 25 width: 0.8em; 26 margin: 0 0.8em 0.2em -1.6em; 27 vertical-align: middle; 28 } 29 .display.math{display: block; text-align: center; margin: 0.5rem auto;} 30 /* CSS for syntax highlighting */ 31 html { -webkit-text-size-adjust: 100%; } 32 pre > code.sourceCode { white-space: pre; position: relative; } 33 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } 34 pre > code.sourceCode > span:empty { height: 1.2em; } 35 .sourceCode { overflow: visible; } 36 code.sourceCode > span { color: inherit; text-decoration: inherit; } 37 div.sourceCode { margin: 1em 0; } 38 pre.sourceCode { margin: 0; } 39 @media screen { 40 div.sourceCode { overflow: auto; } 41 } 42 @media print { 43 pre > code.sourceCode { white-space: pre-wrap; } 44 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } 45 } 46 pre.numberSource code 47 { counter-reset: source-line 0; } 48 pre.numberSource code > span 49 { position: relative; left: -4em; counter-increment: source-line; } 50 pre.numberSource code > span > a:first-child::before 51 { content: counter(source-line); 52 position: relative; left: -1em; text-align: right; vertical-align: baseline; 53 border: none; display: inline-block; 54 -webkit-touch-callout: none; -webkit-user-select: none; 55 -khtml-user-select: none; -moz-user-select: none; 56 -ms-user-select: none; user-select: none; 57 padding: 0 4px; width: 4em; 58 background-color: #ffffff; 59 color: #a0a0a0; 60 } 61 pre.numberSource { margin-left: 3em; border-left: 1px solid #a0a0a0; padding-left: 4px; } 62 div.sourceCode 63 { color: #1f1c1b; background-color: #ffffff; } 64 @media screen { 65 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } 66 } 67 code span { color: #1f1c1b; } /* Normal */ 68 code span.al { color: #bf0303; background-color: #f7e6e6; font-weight: bold; } /* Alert */ 69 code span.an { color: #ca60ca; } /* Annotation */ 70 code span.at { color: #0057ae; } /* Attribute */ 71 code span.bn { color: #b08000; } /* BaseN */ 72 code span.bu { color: #644a9b; font-weight: bold; } /* BuiltIn */ 73 code span.cf { color: #1f1c1b; font-weight: bold; } /* ControlFlow */ 74 code span.ch { color: #924c9d; } /* Char */ 75 code span.cn { color: #aa5500; } /* Constant */ 76 code span.co { color: #898887; } /* Comment */ 77 code span.cv { color: #0095ff; } /* CommentVar */ 78 code span.do { color: #607880; } /* Documentation */ 79 code span.dt { color: #0057ae; } /* DataType */ 80 code span.dv { color: #b08000; } /* DecVal */ 81 code span.er { color: #bf0303; text-decoration: underline; } /* Error */ 82 code span.ex { color: #0095ff; font-weight: bold; } /* Extension */ 83 code span.fl { color: #b08000; } /* Float */ 84 code span.fu { color: #644a9b; } /* Function */ 85 code span.im { color: #ff5500; } /* Import */ 86 code span.in { color: #b08000; } /* Information */ 87 code span.kw { color: #1f1c1b; font-weight: bold; } /* Keyword */ 88 code span.op { color: #1f1c1b; } /* Operator */ 89 code span.ot { color: #006e28; } /* Other */ 90 code span.pp { color: #006e28; } /* Preprocessor */ 91 code span.re { color: #0057ae; background-color: #e0e9f8; } /* RegionMarker */ 92 code span.sc { color: #3daee9; } /* SpecialChar */ 93 code span.ss { color: #ff5500; } /* SpecialString */ 94 code span.st { color: #bf0303; } /* String */ 95 code span.va { color: #0057ae; } /* Variable */ 96 code span.vs { color: #bf0303; } /* VerbatimString */ 97 code span.wa { color: #bf0303; } /* Warning */ 98 </style> 99 <link rel="stylesheet" href="style.css" /> 100 </head> 101 <body> 102 <h1 id="pygame-vs.-raylib">PyGame vs. Raylib</h1> 103 <h2 id="date-20260920">Date: 2026/09/20</h2> 104 <p>I implemented <a href="https://cp-algorithms.com/graph/mst_prim.html">Prim’s algorithm</a> to create a MST of a graph in Python. I also created a visualization of it, shown below.</p> 105 <p><video src="videos/prim.mkv" controls=""><a href="videos/prim.mkv">Video</a></video></p> 106 <p>(This graph has 250 vertices and 1,000 edges. The source code used to generate this visualization can be found <a href="https://git.laack.co/blog/file/python/pygame-vs-raylib/prim.py.html">here</a>)</p> 107 <p>Prim’s algorithm is worth understanding (albeit out of scope), and the problem it solves is finding the set of edges that minimizes the sum of their weights while guaranteeing there is exactly one path between any two vertices in the connected, weighted, undirected graph. In the case of my implementation, the weight of each edge was its length.</p> 108 <p>As it relates to the animation, the following two things should be apparent:</p> 109 <ol type="1"> 110 <li>The first frame must draw every edge and vertex of the graph in a greyed-out color.</li> 111 <li>Each subsequent frame only has to draw one additional edge, the newly traversed edge, and one additional vertex, the vertex we are visiting.</li> 112 </ol> 113 <p>While we could render each frame from scratch based on the graph’s state, any reasonable rendering program has the ability to additively draw geometries to avoid this re-computation, and I care a bit about performance (even though this is written in Python*). Since PyGame and Raylib both support this, I’ll be using additive rendering in both of my implementations.</p> 114 <p>* I did rewrite the animation in C++ using Raylib so I could make it <a href="https://git.laack.co/abg/">my X11 wallpaper</a>, but that’s for another blog post.</p> 115 <h2 id="raylib">Raylib</h2> 116 <h3 id="implementation">Implementation</h3> 117 <p>Raylib has 2D rendered textures that can persist across frames. Here’s some Python code that makes use of them to visualize Prim’s algorithm:</p> 118 <div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pyray <span class="im">as</span> pr</span> 119 <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span> 120 <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ... (other imports, Vertex class definition, Edge class definition)</span></span> 121 <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span> 122 <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>pr.init_window(screen_width, screen_height, <span class="st">"prim"</span>)</span> 123 <span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span> 124 <span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co"># ... (other initialization)</span></span> 125 <span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span> 126 <span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>texture <span class="op">=</span> pr.load_render_texture(screen_width,screen_height)</span> 127 <span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a></span> 128 <span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="op">!</span>done:</span> 129 <span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> first:</span> 130 <span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 131 <span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a></span> 132 <span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># After calling begin_texture_mode(texture) all writes will be </span></span> 133 <span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> <span class="co"># sent to the texture without having to specify we're writing to the </span></span> 134 <span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># texture. This is also how begin_drawing() works, but for frames.</span></span> 135 <span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a></span> 136 <span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a> pr.begin_texture_mode(texture)</span> 137 <span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a> pr.clear_background(pr.BLACK)</span> 138 <span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a></span> 139 <span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> edge_list:</span> 140 <span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(pr.DARKGRAY)</span> 141 <span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vertex <span class="kw">in</span> graph:</span> 142 <span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a> vertex.draw_vertex(pr.DARKGRAY)</span> 143 <span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a></span> 144 <span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a> pr.end_texture_mode()</span> 145 <span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a></span> 146 <span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a> source_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 147 <span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a> dest_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 148 <span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a></span> 149 <span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a> pr.begin_drawing()</span> 150 <span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a> pr.draw_texture_pro(texture.texture, source_rec, dest_rec, pr.Vector2(<span class="dv">0</span>, <span class="dv">0</span>), <span class="dv">0</span>, pr.WHITE)</span> 151 <span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a> pr.end_drawing()</span> 152 <span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a></span> 153 <span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a> first <span class="op">=</span> <span class="va">False</span></span> 154 <span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a></span> 155 <span id="cb1-38"><a href="#cb1-38" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 156 <span id="cb1-39"><a href="#cb1-39" aria-hidden="true" tabindex="-1"></a> first_times.append(end <span class="op">-</span> start)</span> 157 <span id="cb1-40"><a href="#cb1-40" aria-hidden="true" tabindex="-1"></a></span> 158 <span id="cb1-41"><a href="#cb1-41" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span> 159 <span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a></span> 160 <span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a> <span class="co"># ... (perform one step of Prim's algorithm)</span></span> 161 <span id="cb1-44"><a href="#cb1-44" aria-hidden="true" tabindex="-1"></a></span> 162 <span id="cb1-45"><a href="#cb1-45" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="bu">len</span>(to_draw_edge) <span class="op">!=</span> <span class="dv">0</span> <span class="kw">or</span> <span class="bu">len</span>(to_draw_vert) <span class="op">!=</span> <span class="dv">0</span>:</span> 163 <span id="cb1-46"><a href="#cb1-46" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 164 <span id="cb1-47"><a href="#cb1-47" aria-hidden="true" tabindex="-1"></a></span> 165 <span id="cb1-48"><a href="#cb1-48" aria-hidden="true" tabindex="-1"></a> pr.begin_texture_mode(texture)</span> 166 <span id="cb1-49"><a href="#cb1-49" aria-hidden="true" tabindex="-1"></a></span> 167 <span id="cb1-50"><a href="#cb1-50" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> to_draw_edge:</span> 168 <span id="cb1-51"><a href="#cb1-51" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(pr.WHITE)</span> 169 <span id="cb1-52"><a href="#cb1-52" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vert <span class="kw">in</span> to_draw_vert:</span> 170 <span id="cb1-53"><a href="#cb1-53" aria-hidden="true" tabindex="-1"></a> vert.draw_vertex(pr.WHITE)</span> 171 <span id="cb1-54"><a href="#cb1-54" aria-hidden="true" tabindex="-1"></a></span> 172 <span id="cb1-55"><a href="#cb1-55" aria-hidden="true" tabindex="-1"></a> pr.end_texture_mode()</span> 173 <span id="cb1-56"><a href="#cb1-56" aria-hidden="true" tabindex="-1"></a></span> 174 <span id="cb1-57"><a href="#cb1-57" aria-hidden="true" tabindex="-1"></a> source_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 175 <span id="cb1-58"><a href="#cb1-58" aria-hidden="true" tabindex="-1"></a> dest_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 176 <span id="cb1-59"><a href="#cb1-59" aria-hidden="true" tabindex="-1"></a></span> 177 <span id="cb1-60"><a href="#cb1-60" aria-hidden="true" tabindex="-1"></a> pr.begin_drawing()</span> 178 <span id="cb1-61"><a href="#cb1-61" aria-hidden="true" tabindex="-1"></a> pr.draw_texture_pro(texture.texture, source_rec, dest_rec, pr.Vector2(<span class="dv">0</span>, <span class="dv">0</span>), <span class="dv">0</span>, pr.WHITE)</span> 179 <span id="cb1-62"><a href="#cb1-62" aria-hidden="true" tabindex="-1"></a> pr.end_drawing()</span> 180 <span id="cb1-63"><a href="#cb1-63" aria-hidden="true" tabindex="-1"></a></span> 181 <span id="cb1-64"><a href="#cb1-64" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 182 <span id="cb1-65"><a href="#cb1-65" aria-hidden="true" tabindex="-1"></a> redraw_times.append(end <span class="op">-</span> start)</span> 183 <span id="cb1-66"><a href="#cb1-66" aria-hidden="true" tabindex="-1"></a></span> 184 <span id="cb1-67"><a href="#cb1-67" aria-hidden="true" tabindex="-1"></a> to_draw_vert <span class="op">=</span> []</span> 185 <span id="cb1-68"><a href="#cb1-68" aria-hidden="true" tabindex="-1"></a> to_draw_edge <span class="op">=</span> []</span></code></pre></div> 186 <p>Before our main iteration loop we initialize our texture. Then on our first iteration of the loop we draw each of the edges and vertices in our graph. On each subsequent iteration we only draw vertices added to <code>to_draw_vert</code> and edges added to <code>to_draw_edge</code>. Both lists are populated with edges and vertices that were visited since the last frame was rendered.</p> 187 <p>This code is also instrumented to track the amount of time the first frame takes to render, and the amount of time each subsequent frame takes to render as well.</p> 188 <p>Not included above are our Vertex and Edge classes which both implement draw functions. These functions have the following lines to draw geometries with Raylib:</p> 189 <div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>pr.draw_circle(<span class="bu">int</span>(<span class="va">self</span>.x),<span class="bu">int</span>(<span class="va">self</span>.y), RADIUS, c) <span class="co"># vertex</span></span> 190 <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>pr.draw_line(<span class="bu">int</span>(<span class="va">self</span>.v1.x), <span class="bu">int</span>(<span class="va">self</span>.v1.y), <span class="bu">int</span>(<span class="va">self</span>.v2.x), <span class="bu">int</span>(<span class="va">self</span>.v2.y), c) <span class="co"># edge</span></span></code></pre></div> 191 <p>Per the code comment in the larger code snippet, Raylib uses global state to track which canvas is being drawn to, so these functions can be called in texture mode or drawing mode with a similar effect.</p> 192 <h2 id="pygame">PyGame</h2> 193 <h3 id="implementation-1">Implementation</h3> 194 <p>PyGame persists frame state across frames by default so we don’t have to mess with rendered textures like we did with Raylib! Here’s a some code that does the PyGame rendering:</p> 195 <div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a></span> 196 <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># ... (imports and such)</span></span> 197 <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span> 198 <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>pygame.init()</span> 199 <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>display <span class="op">=</span> pygame.display.set_mode((screen_width,screen_height))</span> 200 <span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span> 201 <span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co"># ... (other initialization)</span></span> 202 <span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a></span> 203 <span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> <span class="op">!</span>done:</span> 204 <span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> first:</span> 205 <span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 206 <span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span> 207 <span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> display.fill(BLACK)</span> 208 <span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span> 209 <span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> edge_list:</span> 210 <span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(display, LIGHT_GREY)</span> 211 <span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vertex <span class="kw">in</span> graph:</span> 212 <span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a> vertex.draw_vertex(display, LIGHT_GREY)</span> 213 <span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a></span> 214 <span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a> pygame.display.update()</span> 215 <span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a></span> 216 <span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 217 <span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a> first_time.append(end <span class="op">-</span> start)</span> 218 <span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a></span> 219 <span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a> first <span class="op">=</span> <span class="va">False</span></span> 220 <span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span> 221 <span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a></span> 222 <span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a> <span class="co"># ... (perform one step of Prim's algorithm)</span></span> 223 <span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a></span> 224 <span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 225 <span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a></span> 226 <span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> to_draw_edge:</span> 227 <span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(display, white)</span> 228 <span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vert <span class="kw">in</span> to_draw_vert:</span> 229 <span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a> vert.draw_vertex(display, white)</span> 230 <span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a></span> 231 <span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a> pygame.display.update()</span> 232 <span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 233 <span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a></span> 234 <span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a> to_draw_vert <span class="op">=</span> []</span> 235 <span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a> to_draw_edge <span class="op">=</span> []</span></code></pre></div> 236 <p>I find this code to be more readable than the Raylib code because we don’t have to maintain a standalone texture, and I also like how writing to a display is more explicit in PyGame:</p> 237 <div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>pygame.draw.circle(display, WHITE, (<span class="va">self</span>.x,<span class="va">self</span>.y), RADIUS)</span> 238 <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>pygame.draw.line(display, WHITE, (<span class="va">self</span>.v1.x,<span class="va">self</span>.v1.y), (<span class="va">self</span>.v2.x,<span class="va">self</span>.v2.y), LINE_WIDTH)</span></code></pre></div> 239 <p>(notice the first parameter is the surface to write to)</p> 240 <p>Despite this, the final <code>pygame.display.update()</code> is still using global state, which feels a bit strange given we have to specify which surface we are drawing to.</p> 241 <h2 id="benchmarking">Benchmarking</h2> 242 <p>The benchmarks were executed under the following conditions:</p> 243 <ul> 244 <li>Vertex count: 2,500</li> 245 <li>Edge count: 25,000</li> 246 <li>Screen dimensions: 5120x1440</li> 247 <li>CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz (4 cores, 8 threads)</li> 248 <li>iGPU: Intel Corporation TigerLake-LP GT2 [Iris Xe Graphics]</li> 249 <li>OS: Arch Linux</li> 250 </ul> 251 <p>Additionally, the metrics are all <strong>averages</strong> across 10 full runs.</p> 252 <table> 253 <thead> 254 <tr> 255 <th>Library</th> 256 <th>First frame time (ms)</th> 257 <th>Incremental frame time (ms)</th> 258 </tr> 259 </thead> 260 <tbody> 261 <tr> 262 <td>Raylib</td> 263 <td>29.87 ± 0.74</td> 264 <td>2.88 ± 1.11</td> 265 </tr> 266 <tr> 267 <td>PyGame</td> 268 <td>323.39 ± 44.60</td> 269 <td>5.59 ± 0.65</td> 270 </tr> 271 </tbody> 272 </table> 273 <p>In this benchmark Raylib’s average incremental frame render time was ~2x faster than PyGame’s, and its first frame render time was ~11x faster.</p> 274 <h3 id="why-is-pygame-so-slow">Why Is PyGame So Slow?</h3> 275 <p>Let’s instrument our code to dig deeper:</p> 276 <div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> first:</span> 277 <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 278 <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> display.fill(BLACK)</span> 279 <span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a></span> 280 <span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> edge_times <span class="op">=</span> [] <span class="co"># new</span></span> 281 <span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> vertex_times <span class="op">=</span> [] <span class="co"># new</span></span> 282 <span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span> 283 <span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> edge_list:</span> 284 <span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> se <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 285 <span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(display, LIGHT_GREY)</span> 286 <span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> ee <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 287 <span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> edge_times.append(ee <span class="op">-</span> se) <span class="co"># new</span></span> 288 <span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a></span> 289 <span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vertex <span class="kw">in</span> graph:</span> 290 <span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> sv <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 291 <span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> vertex.draw_vertex(display, LIGHT_GREY)</span> 292 <span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> ev <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 293 <span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> vertex_times.append(ev <span class="op">-</span> sv) <span class="co"># new</span></span> 294 <span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a></span> 295 <span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a> pygame.display.update()</span> 296 <span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 297 <span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a> first_time.append(end <span class="op">-</span> start)</span> 298 <span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a></span> 299 <span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a> first <span class="op">=</span> <span class="va">False</span></span> 300 <span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a></span> 301 <span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Edge time avg: "</span> <span class="op">+</span> <span class="bu">str</span>(<span class="bu">sum</span>(edge_times) <span class="op">/</span> <span class="bu">len</span>(edge_times))) <span class="co"># new </span></span> 302 <span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Vertex time avg: "</span> <span class="op">+</span> <span class="bu">str</span>(<span class="bu">sum</span>(vertex_times) <span class="op">/</span> <span class="bu">len</span>(vertex_times))) <span class="co"># new</span></span> 303 <span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a></span> 304 <span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span></code></pre></div> 305 <p>(Notice the addition of timing for individual edge and vertex draw times for the first frame)</p> 306 <p>Let’s also update our Raylib code to do the same:</p> 307 <div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> first:</span> 308 <span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> start <span class="op">=</span> time.monotonic_ns()</span> 309 <span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> pr.begin_texture_mode(texture)</span> 310 <span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> pr.clear_background(pr.BLACK)</span> 311 <span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a></span> 312 <span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> edge_times <span class="op">=</span> [] <span class="co"># new</span></span> 313 <span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> vertex_times <span class="op">=</span> [] <span class="co"># new</span></span> 314 <span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a></span> 315 <span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> edge <span class="kw">in</span> edge_list:</span> 316 <span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> es <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 317 <span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> edge.draw_edge(pr.DARKGRAY)</span> 318 <span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> ee <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 319 <span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> edge_times.append(ee <span class="op">-</span> es) <span class="co"># new</span></span> 320 <span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> vertex <span class="kw">in</span> graph:</span> 321 <span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> vs <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 322 <span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> vertex.draw_vertex(pr.DARKGRAY)</span> 323 <span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> ve <span class="op">=</span> time.monotonic_ns() <span class="co"># new</span></span> 324 <span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a> vertex_times.append(ve <span class="op">-</span> vs) <span class="co"># new</span></span> 325 <span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a></span> 326 <span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a> pr.end_texture_mode()</span> 327 <span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a></span> 328 <span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a> source_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 329 <span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a> dest_rec <span class="op">=</span> pr.Rectangle(<span class="dv">0</span>, <span class="dv">0</span>, screen_width, screen_height)</span> 330 <span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a></span> 331 <span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> pr.begin_drawing()</span> 332 <span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> pr.draw_texture_pro(texture.texture, source_rec, dest_rec, pr.Vector2(<span class="dv">0</span>, <span class="dv">0</span>), <span class="dv">0</span>, pr.WHITE)</span> 333 <span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> pr.end_drawing()</span> 334 <span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a></span> 335 <span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> end <span class="op">=</span> time.monotonic_ns()</span> 336 <span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a> first_time.append(end <span class="op">-</span> start)</span> 337 <span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a></span> 338 <span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a> first <span class="op">=</span> <span class="va">False</span></span> 339 <span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a></span> 340 <span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Edge time avg: "</span> <span class="op">+</span> <span class="bu">str</span>(<span class="bu">sum</span>(edge_times) <span class="op">/</span> <span class="bu">len</span>(edge_times))) <span class="co"># new</span></span> 341 <span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Vertex time avg: "</span> <span class="op">+</span> <span class="bu">str</span>(<span class="bu">sum</span>(vertex_times) <span class="op">/</span> <span class="bu">len</span>(vertex_times))) <span class="co"># new</span></span> 342 <span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a></span> 343 <span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span></code></pre></div> 344 <p>Running this updated code once we find:</p> 345 <table> 346 <thead> 347 <tr> 348 <th>Library</th> 349 <th>Avg. Edge Draw Time (ns)</th> 350 <th>Avg. Vertex Draw Time (ns)</th> 351 </tr> 352 </thead> 353 <tbody> 354 <tr> 355 <td>PyGame</td> 356 <td>9504.15048</td> 357 <td>686.6508</td> 358 </tr> 359 <tr> 360 <td>Raylib</td> 361 <td>944.15884</td> 362 <td>3016.1852</td> 363 </tr> 364 </tbody> 365 </table> 366 <p>PyGame is faster at drawing vertices (circles), but slower at drawing edges (lines). Since we are drawing many more edges than we are vertices, this contributes to making PyGame slower.</p> 367 <p>PyGame is slower at drawing lines because it uses SDL which computes lines pixel by pixel on the CPU*, while Raylib appends the line ends to a queue for batch processing, to be asynchronously processed by the GPU.</p> 368 <p>In the case of vertices, Raylib calls <code>DrawCircleSector()</code>, and this in turn appends geometries to the GPU drawing queue, but each circle is made of many discrete geometries which are each appended to the queue, costing cycles. Contrasting this with the PyGame approach where PyGame only has to draw a few pixels onto the surface.</p> 369 <p>* There are ways to change the rendering pipeline for PyGame to make better use of GPUs, but these approaches are either complex or experimental.</p> 370 <h4 id="is-that-why-pygame-is-slower">Is that why PyGame is slower?</h4> 371 <p>That’s not the fully story. Let’s consider what the numbers above mean. The first frame renders 2,500 vertices, 25,000 edges. Each subsequent frame renders 1 vertex and 1 edge. If we multiply these numbers with the values we calculated for the amount of time each draw takes, we get the following expected frame times:</p> 372 <table> 373 <colgroup> 374 <col style="width: 33%" /> 375 <col style="width: 33%" /> 376 <col style="width: 33%" /> 377 </colgroup> 378 <thead> 379 <tr> 380 <th>Library</th> 381 <th>Expected First Frame Draw Time (ms)</th> 382 <th>Expected Subsequent Frame Draw Time (ms)</th> 383 </tr> 384 </thead> 385 <tbody> 386 <tr> 387 <td>PyGame</td> 388 <td>239.3204</td> 389 <td>0.0101908</td> 390 </tr> 391 <tr> 392 <td>Raylib</td> 393 <td>31.14443</td> 394 <td>0.003960344</td> 395 </tr> 396 </tbody> 397 </table> 398 <p>Based on this, we see the first frame times are on par with what we actually saw in our benchmarking, but the subsequent frame draw times are orders of magnitude faster than what we benchmarked, and since most frames are subsequent frames, they are severely impacting performance. Let’s see if we can figure out why this disparity exists.</p> 399 <h5 id="pygame-flamegraph">PyGame Flamegraph</h5> 400 <p><img src="images/pygame-flamegraph.svg" style="max-width:100%; height:auto;"></p> 401 <h5 id="raylib-flamegraph">Raylib Flamegraph</h5> 402 <p><img src="images/rlib-flamegraph.svg" style="max-width:100%; height:auto;"></p> 403 <p>We see most of our time is being spent in <code>EndDrawing()</code> for Raylib and <code>pygame.display.update()</code> for Pygame. After instrumenting these calls we find the following times per invocation:</p> 404 <table> 405 <thead> 406 <tr> 407 <th>Library</th> 408 <th>Function</th> 409 <th>Average Call Time (ms)</th> 410 </tr> 411 </thead> 412 <tbody> 413 <tr> 414 <td>PyGame</td> 415 <td><code>EndDrawing()</code></td> 416 <td>5.677199</td> 417 </tr> 418 <tr> 419 <td>Raylib</td> 420 <td><code>pygame.display.update()</code></td> 421 <td>2.680623</td> 422 </tr> 423 </tbody> 424 </table> 425 <p>Based on our original frame time benchmarks, these calls make up almost all of the time spent rendering subsequent frames.</p> 426 <h4 id="what-are-these-functions-doing">What Are These Functions Doing?</h4> 427 <p>In the case of Raylib, EndDrawing does a few things:</p> 428 <ol type="1"> 429 <li>Calls <code>rlDrawRenderBatchActive()</code> 430 <ul> 431 <li>This clears the batched geometries queue, issuing the OpenGL calls</li> 432 </ul></li> 433 <li>Calls <code>SwapScreenBuffer()</code> 434 <ul> 435 <li>This swaps the buffer the display scans out. In effect, this ensures the frame we just drew becomes visible.</li> 436 <li>This is done by with <code>glfwSwapBuffers()</code>, but this is beyond scope as it requires an understanding of OpenGL and double buffering.</li> 437 <li>In the normal case (which is how our code uses it), this function returns after the instructions have been issued, not once the frame has been rendered.</li> 438 </ul></li> 439 <li>Optionally waits to hit the target framerate in cases where rendering is happening faster than the target</li> 440 </ol> 441 <p>Most of the time spent here is on the second step. This is because issuing the instructions to swap will flush the driver’s command stream, and once a few frames are in flight, this will block until the GPU retires one.</p> 442 <p>Let’s step through the PyGame <code>update()</code> function too:</p> 443 <ol type="1"> 444 <li>Calls <code>pg_update()</code> in <code>src_c/display.c</code> 445 <ul> 446 <li>This performs some basic checks to make sure a display is set</li> 447 </ul></li> 448 <li><code>pg_update()</code> calls <code>SDL_UpdateWindowSurfaceRects</code> 449 <ul> 450 <li>This copies the buffer into a buffer owned by the windowing system (X11 in my case)</li> 451 </ul></li> 452 </ol> 453 <p>The main reason this is slower is PyGame copies the whole frame on the CPU for each frame while Raylib’s frame is already in VRAM and the swap doesn’t <a href="https://en.wikipedia.org/wiki/Bit_blit">blit</a>.</p> 454 <h2 id="takeaways">Takeaways</h2> 455 <p>Out of the box Raylib has better performance because it performs hardware accelerated rendering. In terms of ergonomics, they both have their own quirks. Raylib feels like a C library with Python bindings (because it is!), while PyGame feels inconsistent, mixing both explicit surface drawing, and global state for frame updates.</p> 456 <p>Personally, I’m not keen on using PyGame again because I enjoy writing performant software, and the reality of Python is it can’t be performant. This doesn’t mean I won’t ever write Python code again, it’s useful for first passes at a problem, but if I want to productionize that software I’ll have to rewrite it in a sensible programming language. The nice thing about doing a first pass on a project in Raylib is you can then rewrite it in C using Raylib directly, but the same isn’t true for PyGame.</p> 457 </body> 458 </html>