Forum > Other

Fastest way to copy a record or an array of record

(1/1)

Zomis:
I was wondering which the fastest way is to copy a record or an array of record?
Is it onevar:=secondvar that is the fastest, or is it faster to use some kind of CopyMemory function if it's avalible?

I need to copy one record of 33819 bytes
And one static array of 5672823 bytes (871 * 6513 bytes)
This copying will be done many many times, since it's for indexing game levels into a database. For each new item, the old data must be reset to default values.

Manlio:
I don't know why no one ever replied here, but I think it's an interesting subject in many situations, and therefore I'll try to answer it.

And my answer to the question is: MEASURE it.


--- Code: ---
// measure method #1
T := GetTickCount64;
for i := 1 to 1000000 do
  record1 := record2;
T1 := GetTickCount64 - T;

// measure method #2
T := GetTickCount64;
for i := 1 to 1000000 do
  Move(record1, record2, SizeOf(TRecord));
T2 := GetTickCount64 - T;

// and so on... run different methods and compare the time it takes

--- End code ---

I have done similar things in the past and I often found surprising results, e.g. sometimes assigning 0 to each field manually was faster than using FillChar, things like that.

Bottom line, don't rely on guesses or reasoning when you can easily measure.

MarkMLl:

--- Quote from: Manlio on May 30, 2021, 11:18:26 am ---I don't know why no one ever replied here, but I think it's an interesting subject in many situations, and therefore I'll try to answer it.

--- End quote ---

Because the question is 13 years old, the poster left the forum not long after posting it, and the convention is that old threads are not resurrected without /very/ good reason.

MarkMLl

Navigation

[0] Message Index

Go to full version