Re: Optimizing basic operations

new topic     » goto parent     » topic index » view thread      » older message » newer message

As an example of my original question about "optimizing basic operations", you'll find that this code works up to 10% faster than the original bucket_sort, depending on the the number of items (nitems).

	-- Sort s into ascending order. No elements are compared. 
	-- The values of s must be integers from min_value to max_value. 
	sequence count, sorted 
	integer k, offset, c	 
	count = repeat(0, max_value - min_value + 1) 
	offset = min_value - 1 
 
	-- count the number of occurrences of each integer value: 
	for i = 1 to length(s) do 
--              value = s[i] - offset 
--		count[value] += 1 
 
		count[ s[i] - offset ] += 1 
	end for 
 
-- 	sorted = repeat(0, length(s)) 
        sorted = s 
 
	k = 1 
	-- make the resulting sorted sequence 
	for i = 1 to length(count) do 
		c = count[i] 
		if c then 
			sorted[k .. k + c - 1] = i + offset 
--			     s[k .. k + c - 1] = i + offset 
			k += c 
		end if 
	end for 
 
	return sorted 
end function 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu