Developing barrier functions

A barrier function involves refactoring a piece of logic from an existing function into a new, separate function. When it's done, all data required by the new function will be passed as function arguments. Continuing with the preceding example, we can factor out the logic that calculates the doubled sum of data as follows:

function double_sum(data)
total = 0
for v in data
total += 2 * v
end
return total
end

Then, we just modify the original function to make use of this function:

function double_sum_of_random_data(n)
data = random_data(n)
return double_sum(data)
end

Does it really improve performance? Let's run the test:

It turns out to have a huge difference for the Float64 caseā€”the elapsed time went from 347 to 245 microseconds. Comparing the floating-point sum versus integer sum cases, the result also makes perfect sense because summing integers is generally faster than summing floating-point numbers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167