This doesn’t make any sense.
The top time is 4*10^(-7) or 0.0000004; e represents scientific notation: in other words whatever comes after it is what 10 is taken to the power of.
The bottom time is 0.000097
This means that even with your benchmarking, SimpleBit.Add(50,51)
is actually 243 times slower than 50+51
Benchmarking data also tells us the following:
As you can see, in the 50th percentile, your method is 4.7051ms/0.0034ms = 1383 times slower. Note that every iteration was ran 1000 times:
["SimpleBit Add"] = function(Profiler, RandomNumber) -- You can change "Sample A" to a descriptive name for your function
for i = 1, 1000 do
local a = SimpleBit.Add(50, 51)
end
end;
["Default Add"] = function(Profiler, RandomNumber)
for i = 1, 1000 do
local a = 50 + 51
end
end;
This makes sense when you consider that the ALU specifically has an input for ADD. There is no way that trying to replicate addition in Lua code would be able to match the performance of a direct ALU calculation.
Your multiplication method is also very inefficient, especially for larger numbers. There is no need to hard-add numbers together to get the product. Even multiplying numbers like 1,432,551 and 1,321,415 shouldn’t take very long. Consider the following multiplication algorithm on the associated hardware:
Let’s benchmark multiplication:
["SimpleBit Multiply"] = function(Profiler, RandomNumber) -- You can change "Sample A" to a descriptive name for your function
for i = 1, 1000 do
local a = SimpleBit.Multiply(1, 1)
end
end;
["Default Multiply"] = function(Profiler, RandomNumber)
for i = 1, 1000 do
local a = 100 * 123
end
end;
I input the best possible run-time for the SimpleBit Multiplication method (with non-zero input). Here are the results:
I think you get the picture.
While I think it’s cool that you’re learning about how binary arithmetic works (and I just have to say keep learning, it gets super fun and interesting!), this module does not provide any benefit over the traditional *
and +
operations.
Small aside:
Gosh, you made me have to pull out my old notes on binary multiplication, can’t say it wasn’t enjoyable though! Thanks for the unexpected trip down memory lane!