From 99eacd91c8fb1bc07030d95bb8643d3d61a6d7b5 Mon Sep 17 00:00:00 2001 From: resttime Date: Sun, 19 Jan 2020 15:16:46 -0500 Subject: [PATCH] Parallelize code --- src/main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 12560e5..43bdeea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,13 +73,15 @@ fn main() { for j in (0..ny).rev() { for i in 0..nx { - let mut col = Vec3::default(); - for _ in 0..ns { - let u: f32 = (i as f32 + rand_float()) / nx as f32; - let v: f32 = (j as f32 + rand_float()) / ny as f32; - let r = cam.get_ray(u, v); - col += color(r, &world, 0); - } + let mut col: Vec3 = (0..ns) + .into_par_iter() + .map(|_| { + let u: f32 = (i as f32 + rand_float()) / nx as f32; + let v: f32 = (j as f32 + rand_float()) / ny as f32; + let r = cam.get_ray(u, v); + color(r, &world, 0) + }) + .sum(); col /= ns as f32; col = Vec3::new(col[0].sqrt(), col[1].sqrt(), col[2].sqrt());