diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a919a1..9ce464a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ It's about time I started a changelog! This will serve from now on as the master - Add `//forest` command for quickly generating forests - Fix some minor bugs and edge cases - `//subdivide`: Print status update when completing the last chunk + - `//count`: Optimise by removing nested `for` loops ## v1.8: The Quality of Life Update (17th July 2020) diff --git a/worldeditadditions/lib/count.lua b/worldeditadditions/lib/count.lua index b5f1d1e..51f6adc 100644 --- a/worldeditadditions/lib/count.lua +++ b/worldeditadditions/lib/count.lua @@ -16,16 +16,11 @@ function worldeditadditions.count(pos1, pos2) -- z y x is the preferred loop order (because CPU cache I'd guess, since then we're iterating linearly through the data array) local counts = {} - for z = pos2.z, pos1.z, -1 do - for y = pos2.y, pos1.y, -1 do - for x = pos2.x, pos1.x, -1 do - local next = data[area:index(x, y, z)] - if not counts[next] then - counts[next] = 0 - end - counts[next] = counts[next] + 1 - end + for i in area:iterp(pos1, pos2) do + if not counts[data[i]] then + counts[data[i]] = 0 end + counts[data[i]] = counts[data[i]] + 1 end local total = worldedit.volume(pos1, pos2)