READ$/Data Slow
#1
I have some data that I want to keep embedded in the code instead of an external file.  This takes about 4 seconds to execute (the routines formatStarRA, formatStarDec and J2000Topo only account for about 0.3 seconds).  I could make each line (element) one string padding with spaces and then using mid$ to parse it out. That would be 2400 READ$ instead of 2400*11.   Any other suggestions to speed this up? 




FUNCTION DoubleStarLoad AS LONG
    LOCAL t AS LONG
    LOCAL tempRA, TempDec AS DOUBLE, dummy AS STRING

    FOR t= 0 TO 2399
        DoubleStarData(t).CST=READ$((t)*11+1)
        DoubleStarData(t).ObjName=READ$((t)*11+2)
        DoubleStarData(t).SAO=READ$((t)*11+3)
        tempRA=formatStarRA(READ$((t)*11+4))
        tempDec=formatStarDec(READ$((t)*11+5))
        J2000Topo(tempRA,tempDec)
        DoubleStarData(t).RA=tempRA
        DoubleStarData(t).Dec=tempDec
        DoubleStarData(t).Magnitude1=READ$(t*11+6)
        DoubleStarData(t).Magnitude2=READ$(t*11+7)
        DoubleStarData(t).Spectral=READ$(t*11+9)
        DoubleStarData(t).Distance=VAL(READ$(t*11+10))
        DoubleStarData(t).Separation=READ$(t*11+11)
    NEXT t

    DATA "Aqr","1 Aqr","126062","20:39:25","+00:29:11","5.27","12.3","7.03","K0III","71.48","65"
    DATA "Ari","1 Ari","74966","01:49:50","+22:15:45","16","17","1","M3.7+M4.2","","17.5"
    DATA "Ari","1 Ari","74966","01:50:09","+22:16:30","6.33","7.21","0.88","G3III","179.53","2.9"
    DATA "Boo","1 Boo","82942","13:40:40","+19:57:20","5.76","9.6","3.84","A1V","100.6","4.4"
    DATA "Cam","1 Cam","24672","04:32:02","+53:54:39","5.78","6.82","1.04","B0III","217.39","10.4"
    DATA "Del","1 Del","106172","20:30:18","+10:53:45","6.2","8.02","1.82","Be+B","227.79","0.9"
    DATA "Dra","1 Dra","15532","11:31:07","+69:24:00","14.3","14.8","0.5","","","1.2"
    ....

END FUNCTION
Reply
#2
I reformatted the data to one string per line with a fixed length for each element. Much faster - I suppose 11x faster.
Reply
#3
Lots of multiplying by 11. try this:

((this text belongs below the code. See Reason for edit.))
You said "... (the routines formatStarRA, formatStarDec and J2000Topo
only account for about 0.3 seconds).
0.3 seconds may become significant with all *11 gone.
If those functions are only called from DoubleStarLoad, then put their code as GOSUBs within DoubleStarLoad.

Code:
function DoubleStarLoad as long
    local t as long
    local tempRA, TempDec as double, dummy as string

    for t= 0 to 26399 step 11
        DoubleStarData(t).CST=read$(t+1)
        DoubleStarData(t).ObjName=read$(t+2)
        DoubleStarData(t).SAO=read$(t+3)
        tempRA=formatStarRA(read$(t+4)
        tempDec=formatStarDec(read$(t+5)
        J2000Topo(tempRA,tempDec)
        DoubleStarData(t).RA=tempRA
        DoubleStarData(t).Dec=tempDec
        DoubleStarData(t).Magnitude1=read$(t+6)
        DoubleStarData(t).Magnitude2=read$(t+7)
        DoubleStarData(t).Spectral=read$(t+9)
        DoubleStarData(t).Distance=val(read$(t+10)
        DoubleStarData(t).Separation=read$(t+11)
    next t
    'I don't repeat the DATA here.
end function                   

Posted while I was working: " I reformatted the data to one string per line with a fixed length for each element. Much faster - I suppose 11x faster."

You got rid of the *11 another way.
GOSUB suggestion still stands if they're only called from here.

Cheers,
Reply
#4
Populate the UDT array  once and store it as  ASMDATA? 

1.  Write a simple application to populate your array of UDT's and PUT it to a binary file. 
 
2.  Create an ASMDATA block from the file - see below.

3.  Paste the ASMDATA in your application

4.  In your main application, DIM DoubleStarData(num_of_records) AT CODEPTR(myASMData)

Code:
#COMPILE EXE "CreateASMDATA.exe"
#DIM ALL
FUNCTION PBMAIN() AS LONG
    LOCAL sFileName, sTemp,SOut AS STRING
    LOCAL sb,sb2 AS ISTRINGBUILDERA
    LOCAL pB AS BYTE POINTER
    sb = CLASS "StringBuilderA"
    LOCAL ff, lLOF,x AS LONG
    DISPLAY OPENFILE , , ,"Convert File To ASMDATA", "", "", "", "", 0 TO sFIleName
    IF sFilename > "" THEN
        ff = FREEFILE
        OPEN sFIlename FOR BINARY AS #ff
        lLOF = LOF(#ff)
        GET$ #ff, lLOF,sTemp
        CLOSE #ff
        pB = STRPTR(sTemp)
        sb.Capacity = lLOF + 50 ' allow for ASMDATA wrapper
        sFilename = RETAIN$(PATHNAME$(NAMEX,sFileName), ANY CHR$(48 TO 57) + CHR$(65 TO 90) + CHR$(97 TO 122) + "_-")  ' keep characters alowed for names
        sb.Add "ASMDATA " & sFilename & " '" & STR$(lLOF) & " bytes" & $CRLF
        sOut = "DB "
        FOR x = pB TO pB + lLOF -1
          sOut += FORMAT$(PEEK(x)) & ","
          IF (x MOD 32) = 0 THEN
              sOut = RTRIM$(sOut,",")
              sb.Add sOut & $CRLF
              sOut = "DB "
          END IF
        NEXT
        IF sOut > "DB " THEN
           sOut = RTRIM$(sOut,",")
           sb.Add SOut & $CRLF
        END IF
          sb.Add "" & $CRLF & "END ASMDATA" & $CRLF
     END IF
     CLIPBOARD RESET
     CLIPBOARD SET TEXT sb.string
     ? "ASMData " & sFilename & " placed on Clipboard."
END FUNCTION
'
--
New PowerBASIC User Community Forum:
https://pbusers.org/forum
Reply
#5
Thanks for the suggestions. Stuart, that is very clever - I will have to weigh the speed gained vs ease of code maintenance. I have my program initialization down from 12 seconds to 5 seconds - mainly by some initializing happening in a separate thread, so the user isn't staring at the splash screen too long.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)