11const std = @import ("std" );
22
3+ const testing = std .testing ;
34const debug = std .debug ;
45const io = std .io ;
56const math = std .math ;
@@ -9,7 +10,7 @@ const time = std.time;
910
1011const Decl = std .builtin .TypeInfo .Declaration ;
1112
12- pub fn benchmark (comptime B : type ) ! void {
13+ pub fn benchmark (context : anytype , comptime B : type ) ! void {
1314 const args = if (@hasDecl (B , "args" )) B .args else [_ ]void {{}};
1415 const arg_names = if (@hasDecl (B , "arg_names" )) B .arg_names else [_ ]u8 {};
1516 const min_iterations = if (@hasDecl (B , "min_iterations" )) B .min_iterations else 10000 ;
@@ -86,8 +87,8 @@ pub fn benchmark(comptime B: type) !void {
8687 timer .reset ();
8788
8889 const res = switch (@TypeOf (arg )) {
89- void = > @field (B , def .name )(),
90- else = > @field (B , def .name )(arg ),
90+ void = > @field (B , def .name )(context ),
91+ else = > @field (B , def .name )(context , arg ),
9192 };
9293 const runtime = timer .read ();
9394 runtime_sum += runtime ;
@@ -167,7 +168,7 @@ fn alignedPrint(writer: anytype, dir: enum { left, right }, width: u64, comptime
167168}
168169
169170test "benchmark" {
170- try benchmark (struct {
171+ try benchmark (void , struct {
171172 // The functions will be benchmarked with the following inputs.
172173 // If not present, then it is assumed that the functions
173174 // take no input.
@@ -197,15 +198,15 @@ test "benchmark" {
197198 pub const min_iterations = 1000 ;
198199 pub const max_iterations = 100000 ;
199200
200- pub fn sum_slice (slice : []const u8 ) u64 {
201+ pub fn sum_slice (_ : anytype , slice : []const u8 ) u64 {
201202 var res : u64 = 0 ;
202203 for (slice ) | item |
203204 res += item ;
204205
205206 return res ;
206207 }
207208
208- pub fn sum_reader (slice : []const u8 ) u64 {
209+ pub fn sum_reader (_ : anytype , slice : []const u8 ) u64 {
209210 var reader = & io .fixedBufferStream (slice ).reader ();
210211 var res : u64 = 0 ;
211212 while (reader .readByte ()) | c | {
@@ -218,7 +219,7 @@ test "benchmark" {
218219}
219220
220221test "benchmark generics" {
221- try benchmark (struct {
222+ try benchmark (void , struct {
222223 const Vec = @import ("std" ).meta .Vector ;
223224
224225 pub const args = [_ ]type {
@@ -233,7 +234,7 @@ test "benchmark generics" {
233234 "vec16f16" , "vec16f32" , "vec16f64" ,
234235 };
235236
236- pub fn sum_vectors (comptime T : type ) T {
237+ pub fn sum_vectors (_ : anytype , comptime T : type ) T {
237238 const info = @typeInfo (T ).Vector ;
238239 const one = @splat (info .len , @as (info .child , 1 ));
239240 const vecs = [1 ]T {one } ** 512 ;
@@ -246,3 +247,16 @@ test "benchmark generics" {
246247 }
247248 });
248249}
250+
251+ test "benchmark allocating" {
252+ var temp_arena = std .heap .ArenaAllocator .init (testing .allocator );
253+ defer temp_arena .deinit ();
254+
255+ var allocator = temp_arena .allocator ();
256+
257+ try benchmark (& allocator , struct {
258+ pub fn alloc_slice (context : * std.mem.Allocator ) ! []u8 {
259+ return try context .alloc (u8 , 4096 );
260+ }
261+ });
262+ }
0 commit comments