This got me thinking most of my time at work and as soon as I arrived home I started improving the lua code and did some testing, the os.clock() thing didn't really work for my tests because most of the time it would simply return 0.It's not really about substitutions or regex, but about logic, to be honest - you don't need to fix the 'old' version, just understand where it was inefficient for the future, that's why I posted the above. My version was of course built with extracting just a single font from the entire list at once, so when you copy pasted that 10 times, it increased the workload and the time to complete it ten-fold. Also, since we're at it, this applies to everything, including Lua. Lua tables might be very fast, but there too, it will work faster if you work with, iterate, or get individual stuff from a table of just 10 items or less, than from a table of hundreds of items. The time difference to complete the task might not be obvious with the naked eye, but when you use os.clock() to measure start and end times of the operation and compute the difference, it will be noticeable (especially if you repeat the process like 1000 times to see it better).
By the way, jsmorley, you're invited to this conversation, I'd like to have your opinion as well.
I used:
Code:
local startTime = os.clock()for i = fontIndex, endIndex dolocal index = (i % totalFonts) + 1local fontName = getFont(index)SKIN:Bang('!SetOption', 'FontFace'..tostring(item), 'String', fontName)item = item + 1endlocal endTime= (os.clock() - startTime) * 1000print("Time " .. endTime .. " ms")
like this:
Code:
function getFont(index)if fontCache[index] thenreturn fontCache[index]endlocal fontName = ""if index >= 1 and index <= totalFonts thenfontName = fontTable[index]end fontCache[index] = fontNamereturn fontNameend
However, while this is an interesting approach, once you scroll all the fonts, you end up with the same table with 400+ fonts, which doesn't make much sense to me.
I'd like to do what you suggest, using a table that only contains the number of items, in my case this is the totalItems variable, which defaults on 10. What I believe should happen on this table is that it will only store the current 10 fonts, then if there's a new font it should insert it, and remove whatever font is not being displayed anymore, right?
However I'm a little bit lost on that. How should I approach this?
Here's the complete code:
Edit2: After some thought I've ended up with this, I got rid of getFont() function completely.
Code:
includeDuplicates = falseRun = Run or falsefunction Initialize()if Run thenfontIndex = fontIndex or 0totalItems = totalItems or 10fontTable, totalFonts = getWindowsFonts(fontList)SKIN:Bang('!SetVariable', 'FontCount', totalFonts)Run = falseendendfunction Update()for item = 1, totalItems dolocal index = (fontIndex + item - 1) % totalFonts + 1SKIN:Bang('!SetOption', 'FontFace' .. item, 'String', fontTable[index])endreturn fontIndexendfunction getWindowsFonts(fontList)local fonts = {}local seen = includeDuplicates and nil or {}for line in fontList:gmatch("[^\r\n]+") dolocal trimmed = line:match("^%s*(.-)%s*$")if includeDuplicates or not seen[trimmed] thentable.insert(fonts, trimmed)if seen then seen[trimmed] = true endendendreturn fonts, #fontsend
Statistics: Posted by RicardoTM — Today, 3:21 am