<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Benchmark on Cesar Gimenes</title><link>https://crg.eti.br/en/tags/benchmark/</link><description>Recent content in Benchmark on Cesar Gimenes</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>crg@crg.eti.br (Cesar Gimenes)</managingEditor><webMaster>crg@crg.eti.br (Cesar Gimenes)</webMaster><lastBuildDate>Tue, 17 Dec 2024 03:00:24 -0300</lastBuildDate><atom:link href="https://crg.eti.br/en/tags/benchmark/index.xml" rel="self" type="application/rss+xml"/><item><title>Benchmark: Named vs Explicit Returns in Go</title><link>https://crg.eti.br/en/post/benchmark-retornos-nomeados-vs-explicitos-go/</link><pubDate>Tue, 17 Dec 2024 03:00:24 -0300</pubDate><author>crg@crg.eti.br (Cesar Gimenes)</author><guid>https://crg.eti.br/en/post/benchmark-retornos-nomeados-vs-explicitos-go/</guid><description>&lt;p>A question came up in our Go study group: are named returns faster than explicit returns? That was the hypothesis.&lt;/p>
&lt;p>Fortunately, Go has good tools to test this.&lt;/p>
&lt;p>Let&amp;rsquo;s write some code with two sum functions: one with named returns and one with explicit returns.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff7b72">package&lt;/span> main
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff7b72">import&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a5d6ff">&amp;#34;testing&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8b949e;font-style:italic">// Função com retornos nomeados&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff7b72">func&lt;/span> &lt;span style="color:#d2a8ff;font-weight:bold">somaNomeados&lt;/span>(a, b &lt;span style="color:#ff7b72">int&lt;/span>) (resultado &lt;span style="color:#ff7b72">int&lt;/span>, err &lt;span style="color:#ff7b72">error&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> resultado = a &lt;span style="color:#ff7b72;font-weight:bold">+&lt;/span> b
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff7b72">return&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#8b949e;font-style:italic">// Função com retornos explícitos&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#ff7b72">func&lt;/span> &lt;span style="color:#d2a8ff;font-weight:bold">somaExplicitos&lt;/span>(a, b &lt;span style="color:#ff7b72">int&lt;/span>) (&lt;span style="color:#ff7b72">int&lt;/span>, &lt;span style="color:#ff7b72">error&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ff7b72">return&lt;/span> a &lt;span style="color:#ff7b72;font-weight:bold">+&lt;/span> b, &lt;span style="color:#79c0ff">nil&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Next, we create the benchmark functions. The names of these functions matter. Go&amp;rsquo;s testing tool looks for functions that start with &amp;ldquo;Benchmark&amp;rdquo;.&lt;/p></description></item></channel></rss>