On 17/10/16 14:44, Christian Köstlin wrote: > On 17/10/16 14:09, Daniel Kozak via Digitalmars-d-learn wrote: >> Dne 16.10.2016 v 10:41 Christian Köstlin via Digitalmars-d-learn napsal(a): >>> Hi, >>> >>> for an exercise I had to implement a thread safe counter. >>> This is what I came up with: >>> .... >>> >>> btw. I run the code with dub run --build=release >>> >>> Thanks in advance, >>> Christian >> So I have done some testing, on my pc: >> Java result >> counter.AtomicLongCounter@7ff5e7d8 expected: 2000000 got: 1000000 in: 83ms >> counter.ThreadSafe2Counter@59b44e4b expected: 2000000 got: 1000000 in: 77ms >> counter.ThreadSafe1Counter@2e5f6b4b expected: 2000000 got: 1000000 in: >> 154ms >> counter.ThreadUnsafeCounter@762b155d expected: 2000000 got: 730428 in: 13ms >> >> and my D results (code: http://dpaste.com/3QFXACY ): >> snip.AtomicCounter: got: 1000000 expected: 1000000 in 77 ms and 783 μs >> snip.ThreadSafe1Counter: got: 1000000 expected: 1000000 in 287 ms, 727 >> μs, and 3 hnsecs >> snip.ThreadSafe2Counter: got: 1000000 expected: 1000000 in 281 ms, 117 >> μs, and 1 hnsec >> snip.ThreadSafe3Counter: got: 1000000 expected: 1000000 in 158 ms, 480 >> μs, and 2 hnsecs >> snip.ThreadUnsafeCounter: got: 1000000 expected: 1000000 in 6 ms, 682 >> μs, and 1 hnsec >> >> so atomic is same as in Java pthread_mutex is same speed as java >> synchronized >> D mutexes and D synchronized are almost same, I belive that if I could >> setup same attrs as in pthread version it will be around 160ms too. >> >> Unsafe is almost same for D and java. Only java ReentrantLock seems to >> work better. I believe there is some trick, so it will end up not using >> mutexes in the end at all. For example consider this change in D code: >> >> void doIt(alias counter)() { >> auto thg = new ThreadGroup(); >> for (int i=0; i<NR_OF_THREADS; ++i) { >> thg.create(&threadFuncBody!(counter)); >> } >> thg.joinAll(); >> } >> >> change it to >> >> void doIt(alias counter)() { >> auto thg = new ThreadGroup(); >> for (int i=0; i<NR_OF_THREADS; ++i) { >> auto tc = thg.create(&threadFuncBody!(counter)); >> tc.join(); >> } >> } >> >> and results are: >> >> snip.AtomicCounter: got: 1000000 expected: 1000000 in 22 ms, 251 μs, and >> 6 hnsecs >> snip.ThreadSafe1Counter: got: 1000000 expected: 1000000 in 46 ms, 146 >> μs, and 3 hnsecs >> snip.ThreadSafe2Counter: got: 1000000 expected: 1000000 in 44 ms, 961 >> μs, and 5 hnsecs >> snip.ThreadSafe3Counter: got: 1000000 expected: 1000000 in 42 ms, 512 >> μs, and 8 hnsecs >> snip.ThreadUnsafeCounter: got: 1000000 expected: 1000000 in 2 ms, 108 >> μs, and 5 hnsecs >> >> >> >> >> > thank you for looking into it. > this seems to be quite good. > I did expect something in those lines, but got the mentioned numbers on > my os x macbook. perhaps its a os x glitch. > Thanks for the hint about the OS. I rerun the tests on a linux machine, and there everything is fine! linux dlang code: app.AtomicCounter: got: 1000000 expected: 1000000 in 24 ms, 387 μs, and 3 hnsecs app.ThreadSafe1Counter: got: 1000000 expected: 1000000 in 143 ms, 534 μs, and 9 hnsecs app.ThreadSafe2Counter: got: 1000000 expected: 1000000 in 159 ms, 685 μs, and 1 hnsec app.ThreadUnsafeCounter: got: 399937 expected: 1000000 in 9 ms and 556 μs from example got: 156 ms, 198 μs, and 9 hnsecs
linux java code: counter.CounterTest > testAtomicIntCounter STANDARD_OUT counter.AtomicIntCounter@1f2a2347 expected: 1000000 got: 1000000 in: 29ms counter.CounterTest > testAtomicLongCounter STANDARD_OUT counter.AtomicLongCounter@675ad891 expected: 1000000 got: 1000000 in: 24ms counter.CounterTest > testThreadSafe2Counter STANDARD_OUT counter.ThreadSafe2Counter@3043c6d2 expected: 1000000 got: 1000000 in: 38ms counter.CounterTest > testThreadSafeCounter STANDARD_OUT counter.ThreadSafe1Counter@bac4ba3 expected: 1000000 got: 1000000 in: 145ms counter.CounterTest > testThreadUnsafeCounter STANDARD_OUT counter.ThreadUnsafeCounter@2fe82bf8 expected: 1000000 got: 433730 in: 9ms Could someone check the numbers on another OS-X machine? Unfortunately I only have one available. Thanks in advance!