Well my attempts at a better logging system suck... I'm not doing something right or my initial tests were crap.
I'm now getting slower times when using an array to store the strings vs concatenating the strings. Both are fairly fast. Unfortunately, I'm too busy to look at it much more for the next couple of days, but I'm wondering if someone else can either think of a better way or see what I'm doing wrong.
Main.agc:
#include "logger.agc"
#include "debug.agc"
//Benchmark old method using WriteDebug()
numCommits = 1000
numEntries = 1000
for i = 1 to numEntries
CreateSprite(i , 0)
SetSpriteSize(i , 1 , -1)
SetSpriteColor(i , 40 , 40 , 40 , 255)
next i
SetSyncRate(180,1)
totalTime_OldMethod_Entries# = 0.0
totalTime_OldMethodCommits# = 0.0
for i = 1 to numCommits
//string to log - gets large fast!
dOut$ = ""
//some dummy data to log
x# = 0.0
y# = 0.0
t_entries# = timer()
for j = 1 to numEntries
x# = x# + 0.5
if x# > 98
x# = 0.0
endif
y# = y# + 0.75
if y# > 98
y# = 0.0
endif
SetSpritePosition(j , x# , y#)
//since it is often the case to add stuff to the string as we go, this is broken up just for illutration purposes
dOut$ = str(j)
dOut$ = dOut$ + "," + str(x#) + "," + str(y#)
//a new lin must be added so we can read the file
dOut$ = dOut$ + chr(10)
next j
t_entries# = timer() - t_entries#
totalTime_OldMethod_Entries# = totalTime_OldMethod_Entries# + t_entries#
t_Commits# = timer()
on = 1
timeStamp = 1
append = 0
WriteDebug(on, dOut$, timeStamp, append, sWRITE_DEBUG_FILE)
t_Commits# = timer() - t_Commits#
totalTime_OldMethodCommits# = totalTime_OldMethodCommits# + t_Commits#
Print("old method commits = " + str(i))
sync()
next i
averageTime_oldMethodEntries# = totalTime_OldMethod_Entries# / (numCommits * numEntries)
averageTime_oldMethodCommits# = totalTime_OldMethodCommits# / numCommits
totalTime_NewMethod_Entries# = 0.0
totalTime_NewMethodCommits# = 0.0
for i = 1 to numCommits
//string to log - gets large fast!
dOut$ = ""
//some dummy data to log
x# = 0.0
y# = 0.0
t_entries# = timer()
for j = 1 to numEntries
x# = x# + 0.5
if x# > 98
x# = 0.0
endif
y# = y# + 0.75
if y# > 98
y# = 0.0
endif
SetSpritePosition(j , x# , y#)
//with the new method there is no need to add a newline character and we can append to the line as needed
timeStamp = 1
iNewLine0_AppendLine1 = 0
dOut$ = str(j)
DebugLog(dOut$ , timeStamp , iNewLine0_AppendLine1)
//note that now when we append to the same line there's no need to add the dOut$ together
//it is now handle inside of the function
dOut$ = dOut$ + "," + str(x#) + "," + str(y#)
iNewLine0_AppendLine1 = 1
DebugLog(dOut$ , timeStamp , iNewLine0_AppendLine1)
//and there's no longer any need to append a newline character!
next j
t_entries# = timer() - t_entries#
totalTime_NewMethod_Entries# = totalTime_NewMethod_Entries# + t_entries#
t_Commits# = timer()
on = 1
timeStamp = 1
append = 0
DumpDebugLog(append , sDEBUG_LOG_FILE)
t_Commits# = timer() - t_Commits#
totalTime_NewMethodCommits# = totalTime_NewMethodCommits# + t_Commits#
Print("new method commits = " + str(i))
sync()
next i
averageTime_NewMethodEntries# = totalTime_NewMethod_Entries# / (numCommits * numEntries)
averageTime_NewMethodCommits# = totalTime_NewMethodCommits# / numCommits
do
Print("Average Times Old Method:")
Print(" entries = " + str(averageTime_OldMethodEntries#))
Print(" commits = " + str(averageTime_OldMethodCommits#))
Print("")
Print("Average Times New Method:")
Print(" entries = " + str(averageTime_NewMethodEntries#))
Print(" commits = " + str(averageTime_NewMethodCommits#))
Sync()
loop
logger.agc (method I thought was faster)
//A better and faster way!
#constant iDEBUG_DUMP_KEY_APPEND 49 //press 1 key to append to log file
#constant iDEBUG_DUMP_KEY_OVERWRITE 50 //press 2 key to overwrite log file
#constant sDEBUG_LOG_FILE "debug_faster.csv"
global gDebugLogSize as integer
function DebugLog(line$ as string , iTimeStamp , iNewLine0_AppendLine1)
if iNewLine0_AppendLine1 = 1 and gDebugLogSize < 1
Message("Cannot append a line in DebugLog - no lines exist.")
END
endif
if iNewLine0_AppendLine1 = 0
inc gDebugLogSize
dim _debug_log$[gDebugLogSize]
endif
if iToggleTimeStamp = 1
sText$ = GetTimeStampStringMilliseconds() + sText$
elseif iToggleTimeStamp = 2
sText$ = GetTimeStampStringDateTime() + sText$
elseif iToggleTimeStamp = 3
sText$ = str(timer()) + sText$
endif
if len(sText$) = 0 then sText$ = ""
if iNewLine0_AppendLine1 = 0
_debug_log$[gDebugLogSize] = ""
_debug_log$[gDebugLogSize] = sText$
elseif iNewLine0_AppendLine1 = 1
_debug_log$[gDebugLogSize] = _debug_log$[gDebugLogSize] + sText$
endif
endfunction
//call with a null sLogFile$ to just clear out the array
function DumpDebugLog(iAppend as integer , sLogFile$ as string)
success = 0
if sLogFile$ <> ""
if gDebugLogSize <= 0
Message("Can't dump debug log " + sLogFile$ + chr(10) + "_debug_log$ array size = " + str(gDebugLogSize))
EXITFUNCTION success
endif
fileID = OpenToWrite(sLogFile$ , iAppend)
for i = 1 to gDebugLogSize
WriteLine(fileID , _debug_log$[i])
next i
CloseFile(fileID)
endif
gDebugLogSize = 0
undim _debug_log$[]
success = 1
endfunction success
//Use this instead of standard Sync()
//to dump the debug log when you hit the spacebar
function MySync()
if GetRawKeyPressed(iDEBUG_DUMP_KEY_APPEND) = 1
append = 1
DumpDebugLog(append , sDEBUG_LOG_FILE)
endif
if GetRawKeyPressed(iDEBUG_DUMP_KEY_OVERWRITE) = 1
append = 0
DumpDebugLog(append , sDEBUG_LOG_FILE)
endif
Sync()
endfunction
function GetTimeStampStringMilliseconds()
time$ = Str(Getmilliseconds())
maxChar = 19
addZeros = maxChar - len(time$)
zero$ = ""
if addZeros > 0
for i = 1 to addZeros
zero$ = "0" + zero$
next i
endif
time$ = zero$ + time$ + ": "
endfunction time$
function GetTimeStampStringDateTime()
time$ = GetCurrentDate() + "_" + GetCurrentTime() + ": "
endfunction time$
debug.agc (method that I thought was slower)
//My current debug function - simple, but very very slow
#constant sWRITE_DEBUG_FILE "debug_slow.csv"
function WriteDebug(iOnOff as integer, sText$ as string, iToggleTimeStamp as integer, iAppend as integer, sLogFile$ as string)
if iOnOff = 0 then EXITFUNCTION
fileID = OpenToWrite(sLogFile$ , iAppend)
if iToggleTimeStamp = 1
sText$ = GetTimeStampStringMilliseconds() + " -- " + sText$
elseif iToggleTimeStamp = 2
sText$ = GetTimeStampStringDateTime() + sText$
endif
if len(sText$) = 0 then sText$ = ""
WriteLine(fileID , sText$)
CloseFile(fileID)
endfunction
Thoughts?