Cigue
2010-01-29 19:12:34
Do you think it's a good idea to declare all my DTMs (6 of them) locally to save me the hassle of making sure they're there when I need it? or will it slow down the script a lot?
2010-01-29 19:12:34
2010-01-29 19:19:09
2010-01-29 20:51:15
2010-01-29 21:47:14
2010-01-29 22:39:50
function loaddtm(dtm: string):integer;
begin
case lowecase(dtm) of
'dtm1' : result := DTMFromString('78DA63F4626260C8636440054C601226CAE80' +
'6E4371250130EE4971350630FE4571250130AE41710501348847B' +
'5C80FC7CFC6A0004E80581');
'dtm2' : result := DTMFromString('78DA63F4626260C8636440054C601226CAE80' +
'6E4371250130EE4971350630FE4571250130AE41710501348847B' +
'5C80FC7CFC6A0004E80581');
end;
end;
function isdtm(dtm : string):boolean;
begin
result := finddtm(x, y, loaddtm(dtm), blah blah blah);
freedtm(loaddtm(dtm));
end;
begin
if isdtm('dtm1') then writeln('yay :D');
end;
2010-01-29 22:48:27
From: Awkwardsaw
function loaddtm(dtm: string):integer;
begin
case lowecase(dtm) of
'dtm1' : result := DTMFromString('78DA63F4626260C8636440054C601226CAE80' +
'6E4371250130EE4971350630FE4571250130AE41710501348847B' +
'5C80FC7CFC6A0004E80581');
'dtm2' : result := DTMFromString('78DA63F4626260C8636440054C601226CAE80' +
'6E4371250130EE4971350630FE4571250130AE41710501348847B' +
'5C80FC7CFC6A0004E80581');
end;
end;
function isdtm(dtm : string):boolean;
begin
result := finddtm(x, y, loaddtm(dtm), blah blah blah);
freedtm(loaddtm(dtm));
end;
begin
if isdtm('dtm1') then writeln('yay :D');
end;
etc :)
remember to free dtms
program New;
begin
writeln(DTMFromString('78DA635CC2C8C0F0940105DCBD79094C3342F98C6540D67C5435FFFFFF475103003C23082A'));
writeln(DTMFromString('78DA635CC2C8C0F0940105DCBD79094C3342F98C6540D67C5435FFFFFF475103003C23082A'));
end.2010-01-29 23:49:44
2010-01-30 00:10:31
From: Smartzkid
That doesn't work.program New;
begin
writeln(DTMFromString('78DA635CC2C8C0F0940105DCBD79094C3342F98C6540D67C5435FFFFFF475103003C23082A'));
writeln(DTMFromString('78DA635CC2C8C0F0940105DCBD79094C3342F98C6540D67C5435FFFFFF475103003C23082A'));
end.
Even though they are the same DTM, they are loaded separately.
2010-01-30 01:13:14
function isdtm(dtm : string):boolean;
var
tmpdtm: integer;
begin
tmpdtm := loaddtm(dtm);
result := finddtm(x, y, tmpdtm, blah blah blah);
freedtm(tmpdtm);
end;2010-01-30 03:50:55
2010-01-30 08:16:54
From: Smartzkid
The program I posted prints out the memory handle to each DTM. Notice that the two are different? This means each call to DTMFromString loads the DTM to a new place in memory, even though the data is the same.
I can understand your confusion; while the code 'works', it is a memory leak:
You load the DTM into memory pos 1, and search for it.
Next, you load it into memory pos 2, and free it.
The DTM in position 1 never gets freed.
Luckily, a better solution is not far away:function isdtm(dtm : string):boolean;
var
tmpdtm: integer;
begin
tmpdtm := loaddtm(dtm);
result := finddtm(x, y, tmpdtm, blah blah blah);
freedtm(tmpdtm);
end;
The problem now, though, is that you are allocating memory, filling it up with data, and then freeing it - every time you search for a DTM. Memory operations like this take a good amount of time, so if you are using a DTM often, you should consider making it global and keeping it in memory, which will boost your script's efficiency and lower your CPU usage.
In addition, case statements in SCAR are very slow. They act as large if/else statements; thus, as your list of DTMs grows, so will the time it takes to load each one. This is an additional reason why you may want to keep some DTMs global.
2010-01-30 08:49:06
From: AwkwardsawPlease do not take this the wrong way, but you should know this already.. :/
only one dtm is being called per function use, because of the case, how would both of them be called in memory?
also, the case statements "slowness" isn't noticeable at all, even then a little "pause" might be better anyways
2010-01-31 02:02:47
From: Nava2
The best option, in my opinion would be using an array of boolean, set them to true as each dtm is set which is allocated by a constant in an array. Then in the script terminate procedure, have a for loop free every loaded dtm.
I'd write some code but I'm on my iPod.
E: oh and the dtms stored in a TIntegerArray associated to the same object. To be honest a record would work here too.
2010-01-31 02:12:02
procedure FreeAllDTMs;
var
i: integer;
begin
try
repeat
FreeDTM(i);
inc(i);
until(false)
except end;
end;var
xxDTMArray: Array of Integer;
procedure DTMFreeRecent(quantity: integer);
//Free the last q DTMs made
var
i, startIndex: integer;
begin
startIndex := High(xxDTMArray) - quantity;
for i := 0 to quantity do
FreeDTM(xxDTMArray[startIndex + i]);
SetLength(xxDTMArray, startIndex);
end;
function DTMCreate(s: String): integer;
//Create a DTM from input string s
var
size: integer;
begin
result := DTMFromString(s);
size := Length(xxDTMArray);
SetLength(xxDTMArray, size + 1);
xxDTMArray[size] := result;
end;
function DTMQuantity: integer;
//Get number of DTMs currently in use
begin
result := Length(xxDTMArray);
end;
procedure DTMFreeAll;
//Free all DTMs
var
i: integer;
begin
for i := 0 to high(xxDTMArray) do
FreeDTM(xxDTMArray[i]);
SetLength(xxDTMArray, 0);
end;