# Comparing Python, Zig, and C

**URL:** <https://ziggit.dev/t/comparing-python-zig-and-c/6883>\
**Category:** Showcase\
**Created:** [November 16, 2024, 12:36am UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883 "2024-11-16T00:36:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramonmeza](https://ziggit.dev/user_avatar/ziggit.dev/ramonmeza/32/3712_2.png) [@ramonmeza](https://ziggit.dev/u/ramonmeza)\
**Post date:** [November 16, 2024, 12:36am UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/1 "2024-11-16T00:36:52Z")

</div>

Hi everyone!

Since my last post on interacting with C from Zig, I’ve been excited to play around more with the C ABI. This time I decided to try to compare the performance between algorithms implemented in C, Python, and Zig, as a fun exercise (and to boast about performance of the languages I love). So far I’ve only implemented Quick Sort, but am excited about the results.

On a set of 1000 random data points, over the course of 100 tests, it’s obvious that C and Zig outperform Python by a landslide. What isn’t so obvious is the result of Zig being faster than C. I know this method of testing isn’t absolute and bullet-proof, but its a fun exercise and I’m sure someone would like to play around with the code to see what results they can get.

> **[GitHub - ramonmeza/python\_vs\_c\_vs\_zig: A performance comparison of different algorithms...](https://github.com/ramonmeza/python_vs_c_vs_zig)**
>
> A performance comparison of different algorithms implemented in Python, C, and Zig.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [November 16, 2024, 2:07am UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/2 "2024-11-16T02:07:37Z")

</div>

By default zig compiles for your processor (`cpu=native`). You can use the `cpu=baseline` that means a really old cpu.

The build commands are:

- `zig build -Dcpu=baseline`
- `zig build-exe -mcpu=baseline`

---

<div class="post-metadata">

**Author:** ![andrewrk](https://ziggit.dev/user_avatar/ziggit.dev/andrewrk/32/7011_2.png) [@andrewrk](https://ziggit.dev/u/andrewrk)\
**Post date:** [November 16, 2024, 2:19am UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/3 "2024-11-16T02:19:00Z")

</div>

Regarding sort, you might also be interested to compare `qsort` from libc vs `std.mem.sortUnstable` from Zig.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [November 16, 2024, 2:15pm UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/4 "2024-11-16T14:15:22Z")

</div>

> [@ramonmeza](#):
>
> What isn’t so obvious is the result of Zig being faster than C

Just a copy-past from [here](https://github.com/dee0xeed/fpaq0p):

```zig
original c++ version (with -O3 g++ option)
$ time ./fpaq0p c ~/CC/enwik8 zz
enwik8 (100000000 bytes) -> zz (61457810 bytes) in 10.98 s.

real 0m11,093s
user 0m10,831s
sys 0m0,172s

zig
$ time ./fpaq0p c ~/CC/enwik8 zz
enwik8 (100000000 bytes) -> zz (61457810 bytes) in 8890 msec

real 0m8,893s
user 0m8,753s
sys 0m0,136s

```

---

<div class="post-metadata">

**Author:** ![ramonmeza](https://ziggit.dev/user_avatar/ziggit.dev/ramonmeza/32/3712_2.png) [@ramonmeza](https://ziggit.dev/u/ramonmeza)\
**Post date:** [November 16, 2024, 5:17pm UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/5 "2024-11-16T17:17:41Z")

</div>

Idk if copy paste is a fair assessment of my code, considering I also test python. Moreover, i have a framework in place for testing more than just a single algorithm 🫡

---

<div class="post-metadata">

**Author:** ![pierrelgol](https://ziggit.dev/user_avatar/ziggit.dev/pierrelgol/32/10233_2.png) [@pierrelgol](https://ziggit.dev/u/pierrelgol)\
**Post date:** [November 16, 2024, 6:18pm UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/6 "2024-11-16T18:18:07Z")

</div>

Hey cool project, just wanted to share this if you want to improve the build script a little 🙂

```zig
    const run_step = b.step("run", "Run the test comparison.");
    run_step.dependOn(&zig_quicksort_artifact.step);
    run_step.dependOn(&c_quicksort_artifact.step);
    const algorithm = &.{ "c_quicksort", "zig_quicksort", "py_quicksort" };
    inline for (algorithm) |algo| {
        const run_args: []const []const u8 = &.{ "python3", "./app.py", "--num-iterations=10", "--num-elements=100000", "--algorithm=" ++ algo };
        const system_cmd = b.addSystemCommand(run_args);
        run_step.dependOn(&system_cmd.step);
    }

```

to directly run the comparison if you want, also you might want to check your build dll import logic in python, because the directories were wrong, aka they were not in `bin` but in `lib` for me so I had to change it (as well as the extension but I was expecting that).

---

<div class="post-metadata">

**Author:** ![ramonmeza](https://ziggit.dev/user_avatar/ziggit.dev/ramonmeza/32/3712_2.png) [@ramonmeza](https://ziggit.dev/u/ramonmeza)\
**Post date:** [November 16, 2024, 7:42pm UTC](https://ziggit.dev/t/comparing-python-zig-and-c/6883/7 "2024-11-16T19:42:33Z")

</div>

Awesome idea! I’m still learning the build system and this tip is great. I’ll definitely be trying this out, thanks for the suggestion
