|
I always used gperf for this, but upvalue beats it. Interestingly, strcmp was faster than memcmp on my machine.
> On Dec 5, 2013, at 8:11 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
>
> Am 05.12.2013 19:59 schröbte Tom N Harris:
>> On Thursday, December 05, 2013 09:45:06 AM Marc Lepage wrote:
>>> I guess I'd still be looping (or if-else-if-ing) over a list of pointers
>>> for which to compare, if I can't statically embed that dispatch as a switch
>>> statement (the pointers vary at runtime). Better than a strcmp, but now I'm
>>> thinking a table of string-to-integer is probably going to be best.
>>
>> The cost of building the table may dominate runtime if the function is called
>> infrequently. So perhaps you'd want to only memoize strings that are being
>> used. And don't forget bsearch if you can promise the C array will be sorted.
>
> Micro benchmark:
> * 10 different options
> * each option name is 10 bytes long.
> * 2000000 calls for each option name (so 20 million calls total)
>
> luaL_checkoption 1.42 seconds
> bsearch + memcmp 1.24 seconds
> bsearch + strcmp 1.29 seconds
> upvalue table 0.94 seconds