I know this is NOT C++, but you guys always say that C++ is so fast.
So, here is the Scala version:
1 2 3 4 5 6 7
|
import collection.mutable.Map
def countDuplicates[T](col: Iterable[T]) = {
val m = Map[T, Int]()
for (elem <- col)
m.put(elem, m.getOrElse(elem, 0) + 1)
m
}
| |
I changed ROmai's code not to print the results (because I don't want to benchmark stdio) and
also to repeat each iteration 50 times. My code was also run 50 times and average was taken.
The results are for a vector of 1 000 000 integers from range 1..9999.
I compiled ROmai's code with gcc 4.2.x and optimized with -O2 and additional processor specific flags (for core2duo).
ROmai's result.
Starting clock...
Time elapsed: 0.1479
Process returned 0 (0x0) execution time : 7.426 s
Press any key to continue.
|
Scala's result:
"C:\Program Files\Java\jdk1.6.0_21\bin\java" -server -Xmx512M -Didea.launcher.port=7537... com.intellij.rt.execution.application.AppMain pl.edu.pw.ii.cmsbb.DuplicatesTest
Average from 50 iterations: 0.11664 s
Process finished with exit code 0
|
I could write the code in just one line, in a functional style, but then it is slightly slower (but still linear and still signifficantly faster than most of the C++ implementations posted here):
|
array groupBy identity map (x => (x._1, x._2.size))
| |
I haven't timed the last master's r0shi implementation because it is not universal - it requires elements to be from a narrow numeric range and not just from any arbitrary set, so this is cheating and should not count ;)