GithubHelp home page GithubHelp logo

kiranwells / c_vs_rust Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.94 MB

Compares the implementation and speed of Rust and C in a fully optimized mandelbrot set generator

License: BSD Zero Clause License

C 45.30% Makefile 0.66% Rust 54.04%

c_vs_rust's Introduction

C vs. Rust: Mandelbrot

This repository is a comparison between C and Rust, using a Mandelbrot fractal generator. The main goal is to see how close Rust can come to the efficiency of a highly optimized (at least in some respects) C program using threads and SIMD compiler intrinsics. Because of this, the Rust version is implemented using unsafe code and raw pointers for the performance-critical portions. The Rust code has been refactored to remove almost all unsafe code without significantly affecting performance (the only remaining unsafe is marking an unreachable branch, and has nothing to do with memory writes).

Results

In testing on my local machine, I was able to get the runtime of both implementations to be almost indistinguishable. The only noticeable performance difference is in the bitmap saving code, which makes up a very small portion of the run time.

Sample execution time for default image rendered at 7680ร—4320 pixels:

$ cargo run --release -- out.bmp -w 7680 -h 4320

Internal timing:
Time:
    Rendering:	3.32 s
    Saving:	0.15 s
    Total:	3.47 s

zsh time command:
cargo run --release -- out.bmp -w 7680 -h 4320  25.35s user 0.19s system 710% cpu 3.596 total

$ ./mandel out.bmp -w 7680 -h 4320

Internal timing:
Time:
    Rendering:	3.54 s
    Saving:	0.01 s
    Total:	3.55 s

zsh time command:
./mandel out.bmp -w 7680 -h 4320  28.20s user 0.21s system 728% cpu 3.901 total

Compiling

Requirements:

Both implementations require advanced vector extensions to run as intended, and the C code is only known to support Linux-based systems.

The C code can be compiled by executing make in the mandel_c folder. The rust code can be compiled by running cargo build --release in the mandel_rs folder.

Usage

Both programs accept arguments describing the image to render, and produce a bitmap output with the specified filename. The arguments accepted by both programs are the same, but the binaries are in slightly different locations. Rust locates the binary in ./target/release/mandel_rs (or can be run with cargo run --release -- [args]), whereas the C is compiled to ./mandel.

Help is printed on failure to parse arguments, or if the --help flag is passed

./mandel --help

Example usage:

./mandel output.bmp

A simple example

./mandel \
  -x -1.1641595843886 \
  -y -0.25057061578732 \
  -z 38.5 \
  -m 50000 \
  -w 1920 \
  -h 1080 \
  -s 0.8 \
  -f 0.05 \
  -o 0.0 \
  -g 1.35 \
  -G -4.0 \
  -b 1.2 \
  -B 10.2 \
  output.bmp

A complex example

c_vs_rust's People

Contributors

kiranwells avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.