first benchmark, 1.6 seconds

This commit is contained in:
Joe Ardent 2025-09-05 16:01:17 -07:00
parent 635585dd09
commit c3f2ae9a56
3 changed files with 128 additions and 19 deletions

104
Cargo.lock generated
View file

@ -2,6 +2,24 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "justerror"
version = "1.1.0"
@ -13,6 +31,21 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "libc"
version = "0.2.175"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.101"
@ -31,6 +64,41 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "syn"
version = "1.0.109"
@ -78,6 +146,7 @@ name = "tinyrender"
version = "0.1.0"
dependencies = [
"justerror",
"rand",
"thiserror",
]
@ -86,3 +155,38 @@ name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.3+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95"
dependencies = [
"wit-bindgen",
]
[[package]]
name = "wit-bindgen"
version = "0.45.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c573471f125075647d03df72e026074b7203790d41351cd6edc96f46bcccd36"
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.106",
]

View file

@ -5,4 +5,5 @@ edition = "2024"
[dependencies]
justerror = "1.1.0"
rand = "0.9.2"
thiserror = "2.0.16"

View file

@ -2,6 +2,7 @@
extern crate justerror;
mod tga;
use rand::Rng;
use tga::*;
mod point;
@ -32,31 +33,34 @@ fn main() {
let h = 64;
let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB);
let ax = 7;
let ay = 3;
let bx = 12;
let by = 37;
let cx = 62;
let cy = 53;
framebuffer.set(ax, ay, WHITE);
framebuffer.set(bx, by, WHITE);
framebuffer.set(cx, cy, WHITE);
let a = Point::new(ax, ay);
let b = Point::new(bx, by);
let c = Point::new(cx, cy);
line(a, b, &mut framebuffer, BLUE);
line(c, b, &mut framebuffer, GREEN);
line(c, a, &mut framebuffer, YELLOW);
line(a, c, &mut framebuffer, RED);
bench(&mut framebuffer);
framebuffer
.write_file("framebuffer.tga", true, true)
.unwrap();
}
fn bench(fb: &mut TGAImage) {
let mut rng = rand::rng();
for _ in 0..1 << 24 {
let ax = rng.random_range(0..fb.width);
let ay = rng.random_range(0..fb.height);
let a = Point::new(ax, ay);
let bx = rng.random_range(0..fb.width);
let by = rng.random_range(0..fb.height);
let b = Point::new(bx, by);
line(
a,
b,
fb,
[rng.random(), rng.random(), rng.random(), rng.random()].into(),
);
}
}
fn line(mut a: Point, mut b: Point, fb: &mut TGAImage, color: TGAColor) {
let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs();
if is_steep {