lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Apr 16, 2010 at 7:39 PM, Valerio Schiavoni
<valerio.schiavoni@gmail.com> wrote:
> Hello,
> profiling my application, the big bottleneck of it comes from this function:
> function string2hex(str)
> assert(str,"String2hex got null argument")
> local h,s =  string.gsub(str, "(.)", function(c) return
> string.format("%02X", string.byte(c)) end)
> return h
> end
>
> The size of input strings can be from 1kb to 1Mb.
> How would you optimize it ?
>
> thanks for any suggestion,
> valerio

Here's a benchmark for various optimizations I've made. The best version so far:

local hex = {};
for i = 0, 255 do
  hex[i] = string.format("%02X", i);
end

function string2hex5 (s)
  return (string.gsub(s, ".", hex));
end


I've attached the benchmark with various versions.

-- 
- Patrick Donnelly
function string2hex1 (s)
  return ("%02X"):rep(#s):format(s:byte(1, #s));
end

function string2hex2 (s)
  return (string.gsub(s, "(.)", function(c) return string.format("%02X", string.byte(c)) end))
end

function string2hex3 (s)
  local gsub, format, byte = string.gsub, string.format, string.byte;
  return (gsub(s, ".", function(c) return format("%02X", byte(c)) end))
end

function string2hex4 (s)
  local gsub, format, byte = string.gsub, string.format, string.byte;
  return (gsub(s, "..?.?.?.?", function(s) return ("%02X"):rep(#s):format(s:byte(1,#s)) end))
end

local hex = {};
for i = 0, 255 do
  hex[i] = string.format("%02X", i);
end

function string2hex5 (s)
  return (string.gsub(s, ".", hex));
end

local clock = os.clock;

a = ("a"):rep(1e7);
collectgarbage "stop"

local t = clock();
for i = 1, 5 do
  a = a.."a";
  string2hex1(a);
end
print(clock() - t);

local t = clock();
for i = 1, 5 do
  a = a.."a";
  string2hex2(a);
end
print(clock() - t);

local t = clock();
for i = 1, 5 do
  a = a.."a";
  string2hex3(a);
end
print(clock() - t);

local t = clock();
for i = 1, 5 do
  a = a.."a";
  string2hex4(a);
end
print(clock() - t);

local t = clock();
for i = 1, 5 do
  a = a.."a";
  string2hex5(a);
end
print(clock() - t);