Skip to content

Commit 96b1210

Browse files
committed
Use references in ranged for loop for performance
Using a reference avoids making an expensive copy of a shared_ptr. I see the following improvements on my machine when running before/after. Timing is in minutes. InOneWeekend before: 4.07 after: 1.83 TheNextWeek before: 2.60 after: 1.96 TheRestOfYourLife before: 2.82 after: 2.44
1 parent 393b8ea commit 96b1210

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@
956956
bool hit_anything = false;
957957
auto closest_so_far = t_max;
958958

959-
for (auto object : objects) {
959+
for (const auto& object : objects) {
960960
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
961961
hit_anything = true;
962962
closest_so_far = temp_rec.t;

books/RayTracingTheNextWeek.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@
667667

668668
output_box = temp_box;
669669

670-
for (auto object : objects) {
670+
for (const auto& object : objects) {
671671
if (!objects[i]->bounding_box(t0, t1, temp_box))
672672
return false;
673673
output_box = surrounding_box(output_box, temp_box);

books/RayTracingTheRestOfYourLife.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@
22362236
auto weight = 1.0/objects.size();
22372237
auto sum = 0.0;
22382238

2239-
for (auto object : objects)
2239+
for (const auto& object : objects)
22402240
sum += weight * object->pdf_value(o, v);
22412241

22422242
return sum;

src/InOneWeekend/hittable_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& re
3939
auto hit_anything = false;
4040
auto closest_so_far = t_max;
4141

42-
for (auto object : objects) {
42+
for (const auto& object : objects) {
4343
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
4444
hit_anything = true;
4545
closest_so_far = temp_rec.t;

src/TheNextWeek/hittable_list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& re
3939
auto hit_anything = false;
4040
auto closest_so_far = t_max;
4141

42-
for (auto object : objects) {
42+
for (const auto& object : objects) {
4343
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
4444
hit_anything = true;
4545
closest_so_far = temp_rec.t;
@@ -62,7 +62,7 @@ bool hittable_list::bounding_box(double t0, double t1, aabb& output_box) const {
6262

6363
output_box = temp_box;
6464

65-
for (auto object : objects) {
65+
for (const auto& object : objects) {
6666
if (!object->bounding_box(t0, t1, temp_box))
6767
return false;
6868
output_box = surrounding_box(output_box, temp_box);

src/TheRestOfYourLife/hittable_list.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& re
4141
auto hit_anything = false;
4242
auto closest_so_far = t_max;
4343

44-
for (auto object : objects) {
44+
for (const auto& object : objects) {
4545
if (object->hit(r, t_min, closest_so_far, temp_rec)) {
4646
hit_anything = true;
4747
closest_so_far = temp_rec.t;
@@ -64,7 +64,7 @@ bool hittable_list::bounding_box(double t0, double t1, aabb& output_box) const {
6464

6565
output_box = temp_box;
6666

67-
for (auto object : objects) {
67+
for (const auto& object : objects) {
6868
if (!object->bounding_box(t0, t1, temp_box))
6969
return false;
7070
output_box = surrounding_box(output_box, temp_box);
@@ -78,7 +78,7 @@ double hittable_list::pdf_value(const vec3& o, const vec3& v) const {
7878
auto weight = 1.0/objects.size();
7979
auto sum = 0.0;
8080

81-
for (auto object : objects)
81+
for (const auto& object : objects)
8282
sum += weight * object->pdf_value(o, v);
8383

8484
return sum;

0 commit comments

Comments
 (0)