Skip to content

Instantly share code, notes, and snippets.

@wandernauta
Created May 10, 2010 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wandernauta/396288 to your computer and use it in GitHub Desktop.
Save wandernauta/396288 to your computer and use it in GitHub Desktop.
import structs/ArrayList
import os/Time
"For vs Each: on the bench \n" println()
/* Prepare a thousand-item List */
l := ArrayList<Int> new()
for (i in 0..300000) {
l add(i)
}
/* Go get 'em, tiger */
"Good ol' foreach " print()
regfor_s, regfor_e: TimeVal
gettimeofday(regfor_s&, null)
for (it in l) {
it = (it * it)
}
gettimeofday(regfor_e&, null)
ms1: Long = ((regfor_e tv_sec - regfor_s tv_sec)*1000) + ((regfor_e tv_usec - regfor_s tv_usec)/1000)
"took %d ms %s" format(ms1, "#" times(ms1)) println()
/* Go get 'em, other tiger */
"Newfangled stuff " print()
each_s, each_e: TimeVal
gettimeofday(each_s&, null)
l each(func (it: Int) {
it = (it * it)
})
gettimeofday(each_e&, null)
ms2: Long = ((each_e tv_sec - each_s tv_sec)*1000) + ((each_e tv_usec - each_s tv_usec)/1000)
"took %d ms %s" format(ms2, "#" times(ms2)) println()
/* Finish up */
"\nAnd there you have it!" println()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment