Hash Maps

This sample is a simple benchmark taken & ported from Doug Bagley's Great Language Shootout Pages.

This source file makes use of the for-, and the range-macro. The forms are

   (for ( <symbol> <expression>) <body>)
  

and

   (range ( <expression> to <expression>)
  

The for-macro allows to iterate over something enumerable, ie. an object, which defines a method elements (), returning an instance of class little.lang.Iterator. The current item may, within for's body, be accessed by the local variable symbol.

The range-macro just creates an instance of little.lang.Range.

This benchmark currently performs rather bad. To excuse LITTLE, you should note, that all containers are implemented in LITTLE at this point. Later, a native version of little.lang.Hashtable might follow ...


(package test)


;
; A Hash Map Benchmark,
; ported from Doug's Great Language Shootout Page.
; 
; In new-style syntax, using macros.
;
(package test)

(using
	little.io.TextOutputStream
	little.lang.System
	little.lang.container.Map)

(class Hash ()

	(__
		"This class is ported from the Doug's Great Language Shootout page."
		"It measures speed of hash table access, but is influenced a lot by"
		"efficiency of strings as well")

	(method run (num) :static
		(__
			"This method actually runs the test."
			"It repeatedly fills a hash with string-to-integer mappings"
			"and resolves these mappings afterwards."
			"As it's first argument, it expects an integer number, giving the"
			"count of iterations to pass.")

		(let ((map (Map:new))
			  (c 0))
			(for (i (range 1 to num))
				(map:put (i:tostring 'hex) i))
			;(map:dump (TextOutputStream:out))
			(for (i (range 1 to num))
				(when (map:get (i:tostring))
					(inc c)))
			((TextOutputStream:out):println "Result=" c)))

	(method main () :static
		(__
			"The main method invokes (self:run num), where"
			"num is the number passed via (System:arguments)")

		(let ((num (((System:arguments):nextElement):toint)))
			(self:run num)))

)