performance - Delphi procedure parameter: var is slower than pointer? -


i've got "send" routine in delphi 6 accepts variable-sized block of data (a fixed-size header followed varying amounts of data) , routine calls sendto() in winsock. i've coded 2 ways, once passed block var (somewhat misleading, works) , once pointer block passed. simple version used benchmarking looks like:

type    header = record destination, serialnumber: integer end;   pheader = ^header;  var   smallblock: record h: header; data: array[1..5] of integer end;   bigblock: record h: header; data: array[1..100] of integer end;  procedure send1(var h: header; size: integer); begin h.destination := 1; // typical header adjustments before sendto() h.serialnumber := 2; sendto(sock, h, size, 0, client, sizeof(client)) end;  procedure send2(p: pheader; size: cardinal); begin p^.destination := 1; p^.serialnumber := 2; sendto(sock, p^, size, 0, client, sizeof(client)) end;  procedure doit1; begin send1(smallblock.h, sizeof(smallblock)); send1(bigblock.h, sizeof(bigblock)); end;  procedure doit2; begin send2(@smallblock, sizeof(smallblock)); send2(@bigblock, sizeof(bigblock)); end; 

the "send" routine called often, many different block sizes, , should fast possible. after doing few runs of simple benchmarks (by timing calls gettickcount), pointer technique (doit2) seems run 3% faster on machine var technique (doit1), although don't see real difference between 2 techniques in object code (not i'm assembler guru).

is 3% illusion due crude benchmarks, or pointer technique beating var technique?

there no performance difference passing var parameter versus pointer parameter. same thing (pass memory address), , compile similar, if not identical, assembly code. benchmarking differences caused issues in benchmarking itself, not in code being benchmarked. gettickcount() not best benchmarking tool, instance. best way time code use external profiler, aqtime.

btw, doit2() test should instead:

procedure doit2; begin   send2(@(smallblock.h), sizeof(smallblock));   send2(@(bigblock.h), sizeof(bigblock)); end; 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -