soul Benchmark {
soul measure(name, iterations, func) {
println("Running benchmark: " + name)
startTime = Time.now()
for i in range(iterations) {
func()
}
endTime = Time.now()
totalTime = endTime - startTime
avgTime = totalTime / iterations
println("Total time: " + totalTime + " seconds")
println("Average time: " + (avgTime * 1000) + " milliseconds")
println("Iterations: " + iterations)
println("Throughput: " + (iterations / totalTime) + " ops/sec")
println("---")
return {
name: name,
totalTime: totalTime,
averageTime: avgTime,
iterations: iterations,
throughput: iterations / totalTime
}
}
soul compare(benchmarks) {
println("Benchmark Comparison:")
fastest = Array.min(benchmarks, soul(b) { return b.averageTime })
Array.forEach(benchmarks, soul(benchmark) {
ratio = benchmark.averageTime / fastest.averageTime
println(benchmark.name + ": " + ratio.toFixed(2) + "x slower")
})
}
}
// Usage
soul testFunction1() {
// Simulate work
result = 0
for i in range(1000) {
result += i
}
return result
}
soul testFunction2() {
// Simulate different work
result = 1
for i in range(100) {
result *= 1.001
}
return result
}
// Run benchmarks
benchmark1 = Benchmark.measure("Test Function 1", 1000, testFunction1)
benchmark2 = Benchmark.measure("Test Function 2", 1000, testFunction2)
// Compare results
Benchmark.compare([benchmark1, benchmark2])