I’m doing the following:
def createDTable
dTable = Array.new
cTable.each_index do |i|
dTable[cTable[i]] = i
end
end
Is there any way this (very simple) code can be optimized, especially for large cTable and sparse dTable ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I was curious so I benchmarked it. (http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html)
I generated random numbers for the C table. Basically it was better speed-wise to use an Array if the values were within the length of the array, and better to use a Hash when they were much larger.
I tried it with 5 different combinations:
So anyway, here’s my alternate code, but you may be just fine.