1. Post #1
    Your local friendly Lua helper!
    bobbleheadbob's Avatar
    July 2011
    484 Posts
    Ok. I REALLY hope this is the right place for this.

    I made a li'l script that turns a minute value into a time of day on a 12 hour clock.

    function ToHoursMinutes(time)
    	local ampm = "AM"
    	local time = tonumber(time)
    	if time >= 1440 then --Don't do more than 1440
    		time = time - 1440
    	end
    	time = time/60 --60 minutes in an hour.
    	local hours, minutes = math.modf(time) --Split hours from minutes
    	if hours >= 12 then
    		ampm = "PM"
    	end
    	if hours > 12 then
    		hours = hours - 12
    	end
    	if hours == 0 then
    		hours = 12
    	end
    	if minutes == nil then --Errors b-gone!
    		minutes = 0
    	end
    	minutes = math.Round(minutes * 60) --Turn raw minutes into actual minutes.
    	if minutes < 10 then
    		minutes = "0"..tostring(minutes) -- 7:07 instead of 7:7
    	end
    	local time = tostring(hours)..":"..tostring(minutes) --Colon. Combine hours & minutes.
    	return time, ampm
    end

    The input is any number. Probably below 1440, since that's the amount of minutes in a day. It pumps out two outputs: The time and AM or PM. Here's an example of its use:

    local CurTimeBox = vgui.Create("DFrame")
            CurTimeBox:SetPos(ScrW()/2, ScrH()/2)
            CurTimeBox:SetSize(500, 500)
            CurTimeBox:MakePopup()
    
    local Time = vgui.Create("DLabel")
    	Time:SetParent(CurTimeBox)
            Time:Center()
            local newtime = 1
    	function Time:Think()
    		local time, ampm = ToHoursMinutes(newtime)
                    newtime = newtime + 1
    		self:SetText(time.." "..ampm)
    		self:SizeToContents()
    		self:Center()
    	end
    	Time:SetFont("HUDNumber")
    	Time:SizeToContents()
    	Time:SetTextColor(Color(255, 255, 255, 255))
    

    So you can copy/paste this into your script if you want. I don't need credit or anything.

    Again, sorry if this is in the wrong spot. I actually DID read the sticky.
    Reply With Quote Edit / Delete Reply Windows XP United States Show Events Friendly Friendly x 1 (list)

  2. Post #2
    Gold Member
    Hentie's Avatar
    May 2010
    2,129 Posts
    Have you tried using fmod to optimize your code?

  3. Post #3
    Your local friendly Lua helper!
    bobbleheadbob's Avatar
    July 2011
    484 Posts
    Have you tried using fmod to optimize your code?
    Nope. :P
    What's that?

  4. Post #4
    Blue and Proud Of It.
    Dennab
    September 2011
    961 Posts
    Google it?
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Dumb Dumb x 3 (list)

  5. Post #5
    Skydive's Avatar
    February 2012
    54 Posts
    Google it?
    I Agree, Google Is The Database Of This Planet Itself.
    If you want the answer to it, ask the forum AFTER googling it.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Dumb Dumb x 5 (list)

  6. Post #6
    Your local friendly Lua helper!
    bobbleheadbob's Avatar
    July 2011
    484 Posts
    Have you tried using fmod to optimize your code?
    Oh that.

    Okay. Let me just rewrite the whole damn thing. :P

  7. Post #7
    dylanb5123's Avatar
    January 2011
    226 Posts
    if minutes == nil then --Errors b-gone!
        minutes = 0
    end
    

    Can be made into this for optimization:

    minutes = minutes or 0
    
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 3Dumb Dumb x 1 (list)

  8. Post #8
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    if minutes == nil then --Errors b-gone!
        minutes = 0
    end
    

    Can be made into this for optimization:

    minutes = minutes or 0
    
    Wrong, this is actually slower, the correct optimization would be

    if not minutes then minutes = 0 end
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Agree Agree x 3Disagree Disagree x 1 (list)

  9. Post #9
    S W
    S W's Avatar
    May 2010
    316 Posts
    Wrong, this is actually slower, the correct optimization would be

    if not minutes then minutes = 0 end
    Just curious, is it a significant amount slower or is it one of those things where it could even be in a Think hook and still not really make a difference? I often times use 'or' / 'and' to make a ghetto ternary statement in some of my paint hooks mainly just because I like how it looks. If it is significantly slower than it looks like I'll be changing them all back to if-else statements

  10. Post #10
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    Just curious, is it a significant amount slower or is it one of those things where it could even be in a Think hook and still not really make a difference? I often times use 'or' / 'and' to make a ghetto ternary statement in some of my paint hooks mainly just because I like how it looks. If it is significantly slower than it looks like I'll be changing them all back to if-else statements
    When I tested it a nil compare was up to 5% faster in 90% of the checks.

    While the not check was about 20% faster in ALL cases.

    So, it's not significant but he's wrong still.

  11. Post #11
    "The superior man understands what is right; the inferior man understands what will sell"
    Chessnut's Avatar
    August 2011
    3,555 Posts
    What type of tests are you doing?

    http://trac.caspring.org/wiki/LuaPerformance

    Test #5.

  12. Post #12
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    What type of tests are you doing?

    http://trac.caspring.org/wiki/LuaPerformance

    Test #5.
    Did runtime checks, using SysTime() delta's.