Lower-case as key would slower, and why?

84 views
Skip to first unread message

Jakit Liang

unread,
Jul 20, 2025, 10:51:10β€―AM7/20/25
to lua-l
```
local start
-- Faster
local t1 = {x = 0, y = 0}
start = os.clock() for 1, 50000000 do t1.x = t1.x + 1 t1.y = t1.y + 1 end
print(os.clock() - start) -- Slower local t2 = {X = 0, Y = 0}
start = os.clock()Β Β  for 1, 50000000Β Β  do t2.X = t2.X + 1 t2.Y = t2.Y + 1 end
print(os.clock() - start)
```

In this case, why the second one runs slower?

Sainan

unread,
Jul 20, 2025, 11:03:34β€―AM7/20/25
to lu...@googlegroups.com
Both execute well within margin of error within each other for me, if anything the second loop runs faster because the CPU is warmed up by then...

Anyway, too many factors of other programs running, thermals, CPU utilisation, the universe's background radiation, what Zeus ate for breakfast today... don't try to optimise on such a trivial detail. The biggest gains are from using better/faster algorithms.

--Β Sainan

blog...@gmail.com

unread,
Jul 20, 2025, 12:16:44β€―PM7/20/25
to lua-l
The difference is at the level of error, the first test is normal, the second cycles have swapped places.

```
cat test.lua ; alua test.lua ; cat swap_tests.lua ; alua swap_tests.lua
local t1 = {x = 0, y = 0}
local t2 = {X = 0, Y = 0}
start = os.clock() for i = 1, 50000000 do t1.x = t1.x + 1 t1.y = t1.y + 1 end
print(os.clock() - start)
start = os.clock() for i = 1, 50000000 do t2.X = t2.X + 1 t2.Y = t2.Y + 1 end
print(os.clock() - start)
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ luajit β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
0.075292
0.063305
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.1 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
4.589889
4.608593
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.2 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
4.537976
4.585994
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.3 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
3.79108
3.852186
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.4 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
3.17556
3.153118
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.5 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
2.996931
3.008384
local t1 = {x = 0, y = 0}
local t2 = {X = 0, Y = 0}
start = os.clock() for i = 1, 50000000 do t2.X = t2.X + 1 t2.Y = t2.Y + 1 end
print(os.clock() - start)
start = os.clock() for i = 1, 50000000 do t1.x = t1.x + 1 t1.y = t1.y + 1 end
print(os.clock() - start)

β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ luajit β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
0.070102
0.066586
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.1 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
4.773055
4.861655
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.2 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
4.388561
4.395681
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.3 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
3.605546
3.91049
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.4 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
2.996931
3.008384
β˜Ίβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ lua5.5 β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β˜Ί
4.366968
4.363553
```
If you disable garbage collection, the values are about the same. I have a dynamic processor frequency, I ran tests 4 times, the difference for the same case is 0.5 seconds, depending on the frequency and load of the processor. In short, I do not see a correlation between speed and UP/low
Π²ΠΎΡΠΊΡ€Π΅ΡΠ΅Π½ΡŒΠ΅, 20 июля 2025β€―Π³. Π² 18:03:34 UTC+3, Sainan:

Jakit Liang

unread,
Jul 20, 2025, 1:00:38β€―PM7/20/25
to lua-l
Thanks for your testing result! It is useful for me.
Reply all
Reply to author
Forward
0 new messages