3
votes

Which of the following loops will run quicker in a Redis Lua Script for 10,000 iterations. Or will they both run at the same speed.

Does accessing a redis key inside a lua script take the same amount of time as accessing a local variable, e.g a value at a specific index of a table.

local members = redis.pcall('smembers','10000memberset')

for i=1,table.getN(members) do

 local value = members[i]
  -- do some logic on the value

end

or

for i=1,10000 do

  local value = redis.pcall('get',i) 
  -- do some logic on the value

end

Thanks!

1
run the code, time it and let us know the result... - Itamar Haber
will do, just wanted to post and see if anyone had experimented with this. - Joe Andrews
Guessing only, but I'd say that the loop will be more expensive - Itamar Haber
Anyway, you shouldn't do expensive tasks in Lua, or you'll block the entire Redis server for miliseconds or even seconds.. - Matías Fidemraizer
After testing with a 1,000,000 item set and 1,000,000 keys in redis the second loop is 6 times quicker. 20.5s vs 3.1s - Joe Andrews

1 Answers

4
votes

After testing with a 1,000,000 item set and 1,000,000 keys in redis the second loop is 6 times quicker. 20.5s vs 3.1s.