4949/// wave benchmark (SOA) - scalar version: 3.7832s, zmath version: 0.3642s
5050///
5151/// -------------------------------------------------------------------------------------------------
52- pub fn main () ! void {
53- var gpa = std .heap .GeneralPurposeAllocator (.{}){};
54- defer _ = gpa .deinit ();
55- const allocator = gpa .allocator ();
52+ pub fn main (init : std.process.Init ) ! void {
53+ const allocator = init .gpa ;
54+ const io = init .io ;
5655
5756 // m = mul(ma, mb); data set fits in L1 cache; AOS data layout.
58- try mat4MulBenchmark (allocator , 100_000 );
57+ try mat4MulBenchmark (allocator , io , 100_000 );
5958
6059 // v = 0.01 * cross3(va, vb) + vec3(1.0); data set fits in L1 cache; AOS data layout.
61- try cross3ScaleBiasBenchmark (allocator , 10_000 );
60+ try cross3ScaleBiasBenchmark (allocator , io , 10_000 );
6261
6362 // v = dot3(va, vb) * (0.1 * cross3(va, vb) + vec3(1.0)); data set fits in L1 cache; AOS data layout.
64- try cross3Dot3ScaleBiasBenchmark (allocator , 10_000 );
63+ try cross3Dot3ScaleBiasBenchmark (allocator , io , 10_000 );
6564
6665 // q = qmul(qa, qb); data set fits in L1 cache; AOS data layout.
67- try quatBenchmark (allocator , 10_000 );
66+ try quatBenchmark (allocator , io , 10_000 );
6867
6968 // d = sqrt(x * x + z * z); y = sin(d - t); SOA layout.
70- try waveBenchmark (allocator , 1_000 );
69+ try waveBenchmark (allocator , io , 1_000 );
7170}
7271
7372const std = @import ("std" );
74- const time = std .time ;
75- const Timer = time .Timer ;
73+ const Clock = std .Io .Clock ;
7674const zm = @import ("zmath" );
7775
7876var prng = std .Random .DefaultPrng .init (0 );
7977const random = prng .random ();
8078
81- noinline fn mat4MulBenchmark (allocator : std.mem.Allocator , comptime count : comptime_int ) ! void {
79+ noinline fn mat4MulBenchmark (allocator : std.mem.Allocator , io : std.Io , comptime count : comptime_int ) ! void {
8280 std .debug .print ("\n " , .{});
8381 std .debug .print ("{s:>42} - " , .{"matrix mul benchmark (AOS)" });
8482
8583 var data0 = try std .ArrayList ([16 ]f32 ).initCapacity (allocator , 64 );
86- defer data0 .deinit ();
84+ defer data0 .deinit (allocator );
8785 var data1 = try std .ArrayList ([16 ]f32 ).initCapacity (allocator , 64 );
88- defer data1 .deinit ();
86+ defer data1 .deinit (allocator );
8987
9088 var i : usize = 0 ;
9189 while (i < 64 ) : (i += 1 ) {
@@ -118,8 +116,7 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
118116
119117 {
120118 i = 0 ;
121- var timer = try Timer .start ();
122- const start = timer .lap ();
119+ const start = Clock .now (.awake , io );
123120 while (i < count ) : (i += 1 ) {
124121 for (data1 .items ) | b | {
125122 for (data0 .items ) | a | {
@@ -145,16 +142,15 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
145142 }
146143 }
147144 }
148- const end = timer . read ( );
149- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
145+ const end = Clock . now ( .awake , io );
146+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
150147
151148 std .debug .print ("scalar version: {d:.4}s, " , .{elapsed_s });
152149 }
153150
154151 {
155152 i = 0 ;
156- var timer = try Timer .start ();
157- const start = timer .lap ();
153+ const start = Clock .now (.awake , io );
158154 while (i < count ) : (i += 1 ) {
159155 for (data1 .items ) | b | {
160156 for (data0 .items ) | a | {
@@ -165,20 +161,23 @@ noinline fn mat4MulBenchmark(allocator: std.mem.Allocator, comptime count: compt
165161 }
166162 }
167163 }
168- const end = timer .read ();
169- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
164+ const end = Clock .now (
165+ .awake ,
166+ io ,
167+ );
168+ const elapsed_s = @as (f64 , @floatFromInt (start .durationTo (end ).toNanoseconds ())) / std .time .ns_per_s ;
170169
171170 std .debug .print ("zmath version: {d:.4}s\n " , .{elapsed_s });
172171 }
173172}
174173
175- noinline fn cross3ScaleBiasBenchmark (allocator : std.mem.Allocator , comptime count : comptime_int ) ! void {
174+ noinline fn cross3ScaleBiasBenchmark (allocator : std.mem.Allocator , io : std.Io , comptime count : comptime_int ) ! void {
176175 std .debug .print ("{s:>42} - " , .{"cross3, scale, bias benchmark (AOS)" });
177176
178177 var data0 = try std .ArrayList ([3 ]f32 ).initCapacity (allocator , 256 );
179- defer data0 .deinit ();
178+ defer data0 .deinit (allocator );
180179 var data1 = try std .ArrayList ([3 ]f32 ).initCapacity (allocator , 256 );
181- defer data1 .deinit ();
180+ defer data1 .deinit (allocator );
182181
183182 var i : usize = 0 ;
184183 while (i < 256 ) : (i += 1 ) {
@@ -201,8 +200,7 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
201200
202201 {
203202 i = 0 ;
204- var timer = try Timer .start ();
205- const start = timer .lap ();
203+ const start = Clock .now (.awake , io );
206204 while (i < count ) : (i += 1 ) {
207205 for (data1 .items ) | b | {
208206 for (data0 .items ) | a | {
@@ -215,16 +213,15 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
215213 }
216214 }
217215 }
218- const end = timer . read ( );
219- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
216+ const end = Clock . now ( .awake , io );
217+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
220218
221219 std .debug .print ("scalar version: {d:.4}s, " , .{elapsed_s });
222220 }
223221
224222 {
225223 i = 0 ;
226- var timer = try Timer .start ();
227- const start = timer .lap ();
224+ const start = Clock .now (.awake , io );
228225 while (i < count ) : (i += 1 ) {
229226 for (data1 .items ) | b | {
230227 for (data0 .items ) | a | {
@@ -235,14 +232,14 @@ noinline fn cross3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime coun
235232 }
236233 }
237234 }
238- const end = timer . read ( );
239- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
235+ const end = Clock . now ( .awake , io );
236+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
240237
241238 std .debug .print ("zmath version: {d:.4}s\n " , .{elapsed_s });
242239 }
243240}
244241
245- noinline fn cross3Dot3ScaleBiasBenchmark (allocator : std.mem.Allocator , comptime count : comptime_int ) ! void {
242+ noinline fn cross3Dot3ScaleBiasBenchmark (allocator : std.mem.Allocator , io : std.Io , comptime count : comptime_int ) ! void {
246243 std .debug .print ("{s:>42} - " , .{"cross3, dot3, scale, bias benchmark (AOS)" });
247244
248245 var data0 = try std .ArrayList ([3 ]f32 ).initCapacity (allocator , 256 );
@@ -271,8 +268,7 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
271268
272269 {
273270 i = 0 ;
274- var timer = try Timer .start ();
275- const start = timer .lap ();
271+ const start = Clock .now (.awake , io );
276272 while (i < count ) : (i += 1 ) {
277273 for (data1 .items ) | b | {
278274 for (data0 .items ) | a | {
@@ -286,16 +282,15 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
286282 }
287283 }
288284 }
289- const end = timer . read ( );
290- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
285+ const end = Clock . now ( .awake , io );
286+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
291287
292288 std .debug .print ("scalar version: {d:.4}s, " , .{elapsed_s });
293289 }
294290
295291 {
296292 i = 0 ;
297- var timer = try Timer .start ();
298- const start = timer .lap ();
293+ const start = Clock .now (.awake , io );
299294 while (i < count ) : (i += 1 ) {
300295 for (data1 .items ) | b | {
301296 for (data0 .items ) | a | {
@@ -306,14 +301,14 @@ noinline fn cross3Dot3ScaleBiasBenchmark(allocator: std.mem.Allocator, comptime
306301 }
307302 }
308303 }
309- const end = timer . read ( );
310- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
304+ const end = Clock . now ( .awake , io );
305+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
311306
312307 std .debug .print ("zmath version: {d:.4}s\n " , .{elapsed_s });
313308 }
314309}
315310
316- noinline fn quatBenchmark (allocator : std.mem.Allocator , comptime count : comptime_int ) ! void {
311+ noinline fn quatBenchmark (allocator : std.mem.Allocator , io : std.Io , comptime count : comptime_int ) ! void {
317312 std .debug .print ("{s:>42} - " , .{"quaternion mul benchmark (AOS)" });
318313
319314 var data0 = try std .ArrayList ([4 ]f32 ).initCapacity (allocator , 256 );
@@ -342,8 +337,7 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
342337
343338 {
344339 i = 0 ;
345- var timer = try Timer .start ();
346- const start = timer .lap ();
340+ const start = Clock .now (.awake , io );
347341 while (i < count ) : (i += 1 ) {
348342 for (data1 .items ) | b | {
349343 for (data0 .items ) | a | {
@@ -357,16 +351,15 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
357351 }
358352 }
359353 }
360- const end = timer . read ( );
361- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
354+ const end = Clock . now ( .awake , io );
355+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
362356
363357 std .debug .print ("scalar version: {d:.4}s, " , .{elapsed_s });
364358 }
365359
366360 {
367361 i = 0 ;
368- var timer = try Timer .start ();
369- const start = timer .lap ();
362+ const start = Clock .now (.awake , io );
370363 while (i < count ) : (i += 1 ) {
371364 for (data1 .items ) | b | {
372365 for (data0 .items ) | a | {
@@ -377,14 +370,14 @@ noinline fn quatBenchmark(allocator: std.mem.Allocator, comptime count: comptime
377370 }
378371 }
379372 }
380- const end = timer . read ( );
381- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
373+ const end = Clock . now ( .awake , io );
374+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
382375
383376 std .debug .print ("zmath version: {d:.4}s\n " , .{elapsed_s });
384377 }
385378}
386379
387- noinline fn waveBenchmark (allocator : std.mem.Allocator , comptime count : comptime_int ) ! void {
380+ noinline fn waveBenchmark (allocator : std.mem.Allocator , io : std.Io , comptime count : comptime_int ) ! void {
388381 _ = allocator ;
389382 std .debug .print ("{s:>42} - " , .{"wave benchmark (SOA)" });
390383
@@ -394,8 +387,7 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
394387
395388 const scale : f32 = 0.05 ;
396389
397- var timer = try Timer .start ();
398- const start = timer .lap ();
390+ const start = Clock .now (.awake , io );
399391
400392 var iter : usize = 0 ;
401393 while (iter < count ) : (iter += 1 ) {
@@ -428,8 +420,8 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
428420 }
429421 t += 0.001 ;
430422 }
431- const end = timer . read ( );
432- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
423+ const end = Clock . now ( .awake , io );
424+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
433425
434426 std .debug .print ("scalar version: {d:.4}s, " , .{elapsed_s });
435427 }
@@ -445,8 +437,7 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
445437
446438 const scale : f32 = 0.05 ;
447439
448- var timer = try Timer .start ();
449- const start = timer .lap ();
440+ const start = Clock .now (.awake , io );
450441
451442 var iter : usize = 0 ;
452443 while (iter < count ) : (iter += 1 ) {
@@ -469,8 +460,8 @@ noinline fn waveBenchmark(allocator: std.mem.Allocator, comptime count: comptime
469460 }
470461 vt += zm .splat (T , 0.001 );
471462 }
472- const end = timer . read ( );
473- const elapsed_s = @as (f64 , @floatFromInt (end - start )) / time .ns_per_s ;
463+ const end = Clock . now ( .awake , io );
464+ const elapsed_s = @as (f64 , @floatFromInt (start . durationTo ( end ). toNanoseconds ())) / std . time .ns_per_s ;
474465
475466 std .debug .print ("zmath version: {d:.4}s\n " , .{elapsed_s });
476467 }
0 commit comments