<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>C on FastDataScience.eu</title>
    <link>https://fastdatascience.eu/tags/c/</link>
    <description>FastDataScience.eu (C)</description>
    <generator>Hugo -- gohugo.io</generator>
    <copyright>en-us</copyright>
    <lastBuildDate>Tue, 15 Oct 2024 00:00:00 +0000</lastBuildDate>
    
    <atom:link href="https://fastdatascience.eu/tags/c/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Comparing speed of some fast languages</title>
      <link>https://fastdatascience.eu/post/2024-10-18-zig_speed/</link>
      <pubDate>Tue, 15 Oct 2024 00:00:00 +0000</pubDate>
      <guid>https://fastdatascience.eu/post/2024-10-18-zig_speed/</guid>
      <description>&lt;p&gt;To get more familiar with Rust, I&amp;rsquo;ve lately been revisting last year&amp;rsquo;s
Advent of Code problems, which I did in Go last December. My
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/main.go&#34;&gt;Go solution&lt;/a&gt;
solution for &lt;a href=&#34;https://adventofcode.com/2023/day/5&#34;&gt;Day 5&lt;/a&gt; uses brute-force
and is not very clever, but runs fast enough in Go (under 6 minutes). The
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/main.rs&#34;&gt;Rust equivalent&lt;/a&gt;
runs in 1/3 less time, and I was wondering how other languages would fare.
The results might surprise you.&lt;/p&gt;
&lt;p&gt;I ended up writing the solution in
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/main.rs&#34;&gt;Rust&lt;/a&gt;,
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/zig/src/main.zig&#34;&gt;Zig&lt;/a&gt;, and
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/day05.c&#34;&gt;C&lt;/a&gt;,
in addition to the original
&lt;a href=&#34;https://github.com/andreaskaempf/adventofcode/blob/main/2023/day05/main.go&#34;&gt;Go solution&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The problem (at least my naive solution) is good for a language benchmark
because it involves lots of repeated transformations through a series
of logic steps, with math, logic, and branching/looping. It took 5:40
minutes in Go, and Rust brought that down to 3:45 (when compiled with
the &amp;ndash;release flag). The results for the four languages are interesting
(times in minutes):&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://fastdatascience.eu/images/blog/2024-10-18-times.png&#34; alt=&#34;Times&#34;&gt;&lt;/p&gt;
&lt;p&gt;Go takes 50% longer than Rust, better than I expected. As you&amp;rsquo;d expect, C is
faster than Rust, and at 2:40 by quite a margin (compiled using gcc with -O3
flag for optimizations, and clang did even better at 2:21). And the
big surprise was Zig, which ran in 1:51, using its ReleaseFast optimization
flag(i.e., &lt;code&gt;zig build -Doptimize=ReleaseFast&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&#34;the-cost-of-speed-lines-of-code&#34; &gt;The cost of speed: lines of code?
&lt;span&gt;
    &lt;a href=&#34;#the-cost-of-speed-lines-of-code&#34;&gt;
        &lt;svg viewBox=&#34;0 0 28 23&#34; height=&#34;100%&#34; width=&#34;19&#34; xmlns=&#34;http://www.w3.org/2000/svg&#34;&gt;&lt;path d=&#34;M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71&#34; fill=&#34;none&#34; stroke-linecap=&#34;round&#34; stroke-miterlimit=&#34;10&#34; stroke-width=&#34;2&#34;/&gt;&lt;path d=&#34;M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71&#34; fill=&#34;none&#34; stroke-linecap=&#34;round&#34; stroke-miterlimit=&#34;10&#34; stroke-width=&#34;2&#34;/&gt;&lt;/svg&gt;
    &lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;&lt;p&gt;Another comparison is lines of code. You&amp;rsquo;d expect Rust to require fewer lines
of code because of its expressive syntax, with function chaining, functional
programming (map, filter, etc.), macros and the like. This is indeed the
case. C and Zig are lower level, and require more lines (and programming time)
to produce results:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://fastdatascience.eu/images/blog/2024-10-18-lines.png&#34; alt=&#34;Lines of code&#34;&gt;&lt;/p&gt;
&lt;p&gt;However, you can see from a scatter plot of these two how Zig blows the other
languages out of the water from an execution time point of view, while requiring
almost 20% fewer lines than C, and roughly the same as Go:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://fastdatascience.eu/images/blog/2024-10-18-scatter.png&#34; alt=&#34;Lines vs time&#34;&gt;&lt;/p&gt;
&lt;p&gt;Note that these line counts exclude comments and blank lines.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
