Skip to content

Instantly share code, notes, and snippets.

@taku0
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save taku0/82bd4fdfd2e9254d2808 to your computer and use it in GitHub Desktop.

Select an option

Save taku0/82bd4fdfd2e9254d2808 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <unordered_map>
#include <chrono>
int main() {
for (int _ = 0; _ < 100; _++) {
auto start = std::chrono::high_resolution_clock::now();
std::unordered_map<std::string, std::string> map;
for (int i = 0; i < 1000000; i++) {
map.insert(std::pair<std::string, std::string>(std::to_string(i), std::to_string(i)));
}
auto finish = std::chrono::high_resolution_clock::now();
std::cout << map.size() << ' ';
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count() << " ms\n";
}
return 0;
}
# g++ (GCC) 4.8.3
g++ -std=c++11 foo.cpp -Wall -Ofast && ./a.out
# java version "1.8.0_31"
# Scala compiler version 2.11.4
scalac -optimize Main.scala && JAVA_OPTS='-server' scala Main
import scala.collection.mutable
object Main extends App {
0.until(100).foreach { _ =>
val start = System.nanoTime()
val map = compute5()
val time = (System.nanoTime() - start) / 1000.0 / 1000.0
println(s"${map.size} $time ms")
}
def compute1() = {
0.until(1000000).map{ i =>
(i.toString(), i.toString())
}.toMap
}
def compute1Par() = {
0.until(1000000).par.map{ i =>
(i.toString(), i.toString())
}.toMap
}
def compute1Lazy() = {
0.until(1000000).view.map{ i =>
(i.toString(), i.toString())
}.toMap
}
def compute2() = {
val map = mutable.HashMap.empty[String, String]
0.until(1000000).foreach{ i =>
map += ((i.toString(), i.toString()))
}
map
}
def compute3() = {
val map = mutable.Map.empty[String, String]
0.until(1000000).foreach{ i =>
map.put(i.toString(), i.toString())
}
map
}
def compute4() = {
val map = new java.util.HashMap[String, String]
0.until(1000000).foreach{ i =>
map.put(i.toString(), i.toString())
}
map
}
def compute5() = {
import java.util.stream._
val toString = new java.util.function.Function[Integer, String] {
def apply(i: Integer) = i.toString
}
IntStream.range(0, 1000000).boxed().collect(Collectors.toMap[Integer, String, String](toString, toString))
}
def compute5Parallel() = {
import java.util.stream._
val toString = new java.util.function.Function[Integer, String] {
def apply(i: Integer) = i.toString
}
IntStream.range(0, 1000000).unordered().parallel().boxed().collect(Collectors.toConcurrentMap[Integer, String, String](toString, toString))
}
def compute6() = {
import java.util.stream._
import java.util.function._
type M = java.util.Map[String, String]
val supplier = new Supplier[M] {
def get() = new java.util.HashMap[String, String]
}
val accumulator = new ObjIntConsumer[M] {
def accept(map: M, value: Int) {
map.put(value.toString, value.toString)
}
}
val combiner = new BiConsumer[M, M] {
def accept(map1: M, map2: M) {
map1.putAll(map2)
}
}
IntStream.range(0, 1000000).unordered().collect(supplier, accumulator, combiner)
}
def compute7() = {
val map = new java.util.concurrent.ConcurrentHashMap[String, String]
0.until(1000000).par.foreach{ i =>
map.put(i.toString(), i.toString())
}
map
}
def compute8() = {
val map = scala.collection.concurrent.TrieMap.empty[String, String]
0.until(1000000).par.foreach{ i =>
map += ((i.toString(), i.toString()))
}
map
}
def compute9() = {
import java.util.stream._
val map = new java.util.concurrent.ConcurrentHashMap[String, String]
val consumer = new java.util.function.IntConsumer {
def accept(i: Int) = map.put(i.toString, i.toString)
}
IntStream.range(0, 1000000).unordered().parallel().forEach(consumer)
map
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment