lua-users home
lua-l archive

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


On Fri, Apr 16, 2010 at 8:32 PM, Patrick Donnelly <batrick@batbytes.com> wrote:
> 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.

Oops, I forgot to leave the first benchmark commented. string2hex1
won't work because of stack limitations.

-- 
- Patrick Donnelly