codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// Code adapted from: // http://alaska-kamtchatka.blogspot.com/2009/05/is-immutable-data-slow.html // Java code public final class MutTest { private static final class Person0 { public final String name; public final int age; public final double balance; public Person0(String name, int age, double balance) { this.name = name; this.age = age; this.balance = balance; } public Person0 deposit(double amount) { return new Person0(this.name, this.age, this.balance + amount); } } private static final class Person1 { public String name; public int age; public double balance; public Person1(String name, int age, double balance) { this.name = name; this.age = age; this.balance = balance; } public void deposit(double amount) { this.balance += amount; } } private interface Test { double test(int iters); } private static final Test TEST0 = new Test() { public double test(int iters) { Person0 person = new Person0("John Doe", 19, 100.0); for (int i = 0; i != iters; i++) { person = person.deposit(1.0); } return person.balance; } }; private static final Test TEST1 = new Test() { public double test(int iters) { Person1 person = new Person1("John Doe", 19, 100.0); for (int i = 0; i != iters; i++) { person.deposit(1.0); } return person.balance; } }; private static int test(int times, Test test, int iters) { long best = Long.MAX_VALUE; double balance; for (int i = 0; i != times; i++) { long now = System.currentTimeMillis(); balance = test.test(iters); now = System.currentTimeMillis() - now; if (best > now) best = now; } return (int)((double) iters / ((double) best / 1000.0)); } public static void main(String[] args) { final int iters = 100000000; System.out.printf("Immutable record: %d updates/s\n", test(5, TEST0, iters)); System.out.printf("Mutable record: %d updates/s\n", test(5, TEST1, iters*4)); } } /* Java server: Immutable record: 48_520_135 updates/s Mutable record: 634_920_634 updates/s (13.1 X) */ ------------------------------------- // D1 code import tango.stdc.stdio: printf; import tango.time.StopWatch: StopWatch; StopWatch elapsed; final class Person0 { final char[] name; final double balance; final int age; this(char[] name, int age, double balance) { this.name = name; this.age = age; this.balance = balance; } final Person0 deposit(double amount) { return new Person0(this.name, this.age, this.balance + amount); } } final class Person1 { char[] name; double balance; int age; this(char[] name, int age, double balance) { this.name = name; this.age = age; this.balance = balance; } final void deposit(double amount) { this.balance += amount; } } public double test0(int iters) { auto person = new Person0("John Doe", 19, 100.0); for (int i = 0; i != iters; i++) person = person.deposit(1.0); return person.balance; } public double test1(int iters) { auto person = new Person1("John Doe", 19, 100.0); for (int i = 0; i != iters; i++) person.deposit(1.0); return person.balance; } int test(TyFun)(int times, TyFun test, int iters) { double best = double.max; double balance; for (int i = 0; i != times; i++) { elapsed.start; balance = test(iters); double now = elapsed.stop; if (best > now) best = now; } return cast(int)(iters / best); } void main() { const int iters = 10_000_000; printf("Immutable record: %d updates/s\n", test(5, &test0, iters)); printf("Mutable record: %d updates/s\n", test(5, &test1, iters*60)); // requires an higher iters! } /* Immutable record: 3_773_527 updates/s Mutable record: 631_569_640 updates/s (167 X) */
Private
[
?
]
Run code