Reading a text file

@John @sim Question. In V3, why does read1 read a text file almost instantly while read2 takes about 3 seconds on the first read. It’s the same file, just accessed different. Once used, they both take about the same time after that.

Why does read2 take so long to find the file initially but read1 doesn’t.

function setup()
    s=require("socket")
    n1="documents"
    n2="Dropbox.assets"
    n3="/test910.txt"
end

function draw()
    background(0)
    fill(255)
    rect(WIDTH/2,500,100,50)
    rect(WIDTH/2,400,100,50)
    fill(0)
    text("read1",WIDTH/2+50,525)
    text("read2",WIDTH/2+50,425)
end

function touched(t)
    if t.state==BEGAN then
        if t.x>400 and t.x<500 and t.y>500 and t.y<550 then
            read1()
        end
        if t.x>400 and t.x<500 and t.y>400 and t.y<450 then
            read2()
        end
    end
end

function read1()
    st=s:gettime()
    str1=readText(asset[n1]..n2..n3)
    en=s:gettime()
    print(en-st.." sec",str1)
end

function read2()
    st=s:gettime()
    str2=readText(asset.documents.Dropbox.test910)
    en=s:gettime()
    print(en-st.." sec",str2)
end or paste code here

If I had to guess, the first version is constructing an asset key directly, while the second one is actually building a cache of every file in each subdirectory (each time you expand via the . syntax). If there’s a large number of files in those directories, maybe the caching is slowing it down?

@John @sim @dave1707 - just tried to set up Dropbox and sync it on my pad. Codea crashed during the sync.

Edit: Examining the Dropbox.assets file that was produced showed all the directory structue involved but with zero files in, but some with empty internal folders. There was also some Codea files in the root but with zero size

@dave1707 @John @sim - just modified the code placing the zero timing before each call and eliminating it from each function. Results were more consistent.


function touched(t)
    if t.state==BEGAN then
        if t.x>400 and t.x<500 and t.y>500 and t.y<550 then
            st=s:gettime()
            read1()
        end
        if t.x>400 and t.x<500 and t.y>400 and t.y<450 then
            st=s:gettime()
            read2()
        end
    end
end

function read1()
  --  st=s:gettime()
    str1=readText(asset[n1]..n2..n3)
    en=s:gettime()
    print(en-st.." sec",str1)
end

function read2()
  --  st=s:gettime()
    str2=readText(asset.documents.Dropbox.adder)
    en=s:gettime()
    print(en-st.." sec",str2)
end 

Edit: Forgot to mention I placed the text file (adder) in the Codea Dropbox.assets file directly.

@Bri_G What do you mean by more consistent. I tried your changes and my results were the same as my original code. read1 was almost instant and read2 was around 3 seconds. I placed my test file in the project folder and both times were almost the same. read2 was slightly slower than read1. What @John said sounded about right because of all the files and projects I have.

@dave1707 - when I first ran your code on my pad the discrepancy was very large. On making the changes they were much closer together, probably downto differences in our systems.

I think its possible to fix this sort of issue but we’d need to restructure the asset caching system so that it wouldn’t need to start from scratch each time (so there would be only a one-time delay). Or maybe there’s a way to improve efficiency overall so even if there’s a delay, it doesn’t last very long

A suggestion: perhaps via the algorithm that determines you are typing a direct asset path, you can parse it to a string first, return that asset if it exists and then start the caching steps.