Best method to load predetermined strings

I’m making an app where i’m going to have around 75 sentences/strings that i will need to load up at any point with a corresponding picture.

What do you think would be the best method of loading these strings with the best performance? Just store it in a variable, array, JSON, etc…? 

Any suggestions would be greatly appreciated thanks.

If you’re only loading 75 strings (sentences), just keep a pre-defined table right in your code.

It only makes sense to worry about loading from persistent-memory (device storage) when we’re talking about 100’s, 1000’s or more.

The performance hit for a table of 75 strings with a a length of say < 200 characters, will be un-detectable.

-Ed

Want to prove this to yourself?  Do this:

main.lua

local function doTest() local start = system.getTimer() local myTable = require "test" local end = system.getTimer() local tmp = display.newText( 40, 40, (end - start), native.systemFont, 10 ) tmp.anchorX = 0 end -- Wait a moment after app start to allow system to settle from other launch actions -- Will give more accurate cost reading. timer.peformWithDelay( 1000, doTest )

In “test.lua”

local myTable = { -- BEGINNING OF 10 Entries "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", -- END OF last 10 } return myTable

Note: In the above sample, I only included 10 lines.  You’ll need to cut-copy-past to get the number entries you want.    I suggest grabbing the groups of 10 and duplicating it over again 9 more times for an even 100 lines.

When you run this, it will print the time in millisecond that it took to load the file.  You’ll see it is very short.  This loading is very close to the same cost your app will experience if you do this at load time.  

With the newText() statement you can test this on devices to prove to yourself it is cheap.

Note: After requiring the test file above the cost is zero for all future loads.

Try this to see that:

local function doTest(y) local start = system.getTimer() local myTable = require "test" local end = system.getTimer() local tmp = display.newText( 40, y, (end - start), native.systemFont, 10 ) tmp.anchorX = 0 end -- Wait a moment after app start to allow system to settle from other launch actions -- Will give more accurate cost reading. timer.peformWithDelay( 1000, function() doTest(40) end ) timer.peformWithDelay( 2000, function() doTest(80) end ) timer.peformWithDelay( 3000, function() doTest(120) end )

thanks i was thinking tables for sure too after some research. do you think i should also just store the image/sprite in the table that is associated or should i do that separately? 

You can’t store image data in a table and use it in Corona.  At least not the way you’re describing it.  

You’ll need to load images from ‘disk’.

thanks a ton thats awesome and i’ll just use an associative table and retrieve an image from the disk with an if statement checking for which string i loaded

If you’re only loading 75 strings (sentences), just keep a pre-defined table right in your code.

It only makes sense to worry about loading from persistent-memory (device storage) when we’re talking about 100’s, 1000’s or more.

The performance hit for a table of 75 strings with a a length of say < 200 characters, will be un-detectable.

-Ed

Want to prove this to yourself?  Do this:

main.lua

local function doTest() local start = system.getTimer() local myTable = require "test" local end = system.getTimer() local tmp = display.newText( 40, 40, (end - start), native.systemFont, 10 ) tmp.anchorX = 0 end -- Wait a moment after app start to allow system to settle from other launch actions -- Will give more accurate cost reading. timer.peformWithDelay( 1000, doTest )

In “test.lua”

local myTable = { -- BEGINNING OF 10 Entries "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", -- END OF last 10 } return myTable

Note: In the above sample, I only included 10 lines.  You’ll need to cut-copy-past to get the number entries you want.    I suggest grabbing the groups of 10 and duplicating it over again 9 more times for an even 100 lines.

When you run this, it will print the time in millisecond that it took to load the file.  You’ll see it is very short.  This loading is very close to the same cost your app will experience if you do this at load time.  

With the newText() statement you can test this on devices to prove to yourself it is cheap.

Note: After requiring the test file above the cost is zero for all future loads.

Try this to see that:

local function doTest(y) local start = system.getTimer() local myTable = require "test" local end = system.getTimer() local tmp = display.newText( 40, y, (end - start), native.systemFont, 10 ) tmp.anchorX = 0 end -- Wait a moment after app start to allow system to settle from other launch actions -- Will give more accurate cost reading. timer.peformWithDelay( 1000, function() doTest(40) end ) timer.peformWithDelay( 2000, function() doTest(80) end ) timer.peformWithDelay( 3000, function() doTest(120) end )

thanks i was thinking tables for sure too after some research. do you think i should also just store the image/sprite in the table that is associated or should i do that separately? 

You can’t store image data in a table and use it in Corona.  At least not the way you’re describing it.  

You’ll need to load images from ‘disk’.

thanks a ton thats awesome and i’ll just use an associative table and retrieve an image from the disk with an if statement checking for which string i loaded