|
| 03 Aug 2013 08:09 AM |
Ok, so I have this code that uses string.rep and math.floor 4 times and string.gsub 9 times and I was wondering how I can make it more efficient (It wont be commonly used by the same ideas may apply to more complex code that is used more commonly)
Take a look at it, use it if you wish:
local getRomanNumeral = function(number) return (('M'):rep(math.floor(number/1000)) .. ('C'):rep(math.floor((number%1000)/100)) .. ('X'):rep(math.floor((number%100)/10)) .. ('I'):rep(math.floor((number%10)/1))):gsub('CCCCC', 'D'):gsub('DCCCC', 'CM'):gsub('CCCC', 'CD'):gsub('XXXXX', 'L'):gsub('LXXXX', 'XC'):gsub('XXXX', 'XL') :gsub('IIIII', 'V'):gsub('VIIII', 'IX'):gsub('IIII', 'IV') end |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 03 Aug 2013 08:21 AM |
| When you say efficient, do you mean speed or readability? |
|
|
| Report Abuse |
|
|
|
| 03 Aug 2013 08:23 AM |
| Speed. Its currently very reliable (Up to 5000, but I doubt anyone will get to round 5000 in my game) |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 03 Aug 2013 08:24 AM |
| I would design different methods to do the same thing and compare their running times. |
|
|
| Report Abuse |
|
|
|
| 03 Aug 2013 08:27 AM |
Well in order to do that I need to know more.
Could I combine multiple parts of that with gsub? I don't like having to use it 9 times (Maybe if I could use it only 3 it would be better) |
|
|
| Report Abuse |
|
|