Skip to content

Commit

Permalink
Merge pull request #12 from hendriknielaender/calc-avg
Browse files Browse the repository at this point in the history
fix: use duration array length for average calculation
  • Loading branch information
hendriknielaender authored Nov 3, 2023
2 parents e83dc4c + 2163b60 commit d358be3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions zbench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,16 @@ pub const Benchmark = struct {

// Calculate the average duration
pub fn calculateAverage(self: Benchmark) u64 {
// prevent a div by zero: check number of ops
const ops = self.totalOperations;
if (ops == 0) return 0;
// prevent division by zero
const len = self.durations.items.len;
if (len == 0) return 0;

var sum: u64 = 0;
for (self.durations.items) |duration| {
sum += duration;
}
const avg = sum / ops; // TODO : make sure n ops == self.durations.items.len

const avg = sum / len;

return avg;
}
Expand Down

0 comments on commit d358be3

Please sign in to comment.