Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #962

Re: Why is java consumer/producer so much faster than C++

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.albasani.net!.POSTED!not-for-mail
From Melzzzzz <mel@zzzzz.com>
Newsgroups comp.lang.c++, comp.programming.threads
Subject Re: Why is java consumer/producer so much faster than C++
Date Mon, 23 Jul 2012 01:28:01 +0200
Organization albasani.net
Lines 67
Message-ID <jui2a3$a3a$2@news.albasani.net> (permalink)
References <juht3o$a3a$1@news.albasani.net> <ba6e5833-f9a2-428e-815b-aad0ebdf3c55@s6g2000pbi.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.albasani.net ag1OGn3xnRX1La+NJDwmda5jlHrFgWfVvWuH792l4Juhi8u6H0ogv0sI2IC4QdEBC5+V1YJ71n8QQohV7rTsmZVG7hzsPbpFYhjY1UyeKPOu2qzefoW4LgFDckH70CNZ
NNTP-Posting-Date Sun, 22 Jul 2012 23:28:03 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="YcijAujngn4EbKC/x3XktWnT/vloiNhOBKSWxoIWkdqG6qrezMDGz/tlgmgXXPEImmGcmEtN2P+A6JVFP8XsvG9dVTPHQoca3FMAm2KdKeupX24HzjmeF55xUGRlkb32"; mail-complaints-to="abuse@albasani.net"
X-Newsreader Claws Mail 3.8.0 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Cancel-Lock sha1:fxd7J/ZcDR1t3CHm/WfSteWgau0=
Xref csiph.com comp.lang.c++:17186 comp.programming.threads:962

Cross-posted to 2 groups.

Show key headers only | View raw


On Sun, 22 Jul 2012 15:42:55 -0700 (PDT)
Joshua Maurice <joshuamaurice@gmail.com> wrote:

> 
> 
> What g++ optimization options did you use? If you didn't compile with
> proper optimization flags (e.g. at least -O2), then the numbers you
> have are meaningless.

I have used -O2.

> 
> Also, you might be testing the difference between std::rand() and
> Java's Random.

Possibly.

> 
> Also, why did you use dynamic allocation in the C++ code for an int?

Actually with non dynamical allocation seems to be slower ;)

> If your original goal was to compare the Java way to the C++ way, you
> are not doing it right. This is especially troubling after you
> apparently went through the effort to make the queue support move-only
> types. (Not sure. I'm still new to move-semantics.)

That was necessary in order to put unique_ptr in queue.

> 
> Also,
> >         std::unique_ptr<int> arr[size];
> ...
> >             arr[std::rand()%size] = produced.take();
> I wonder if it's possible to optimize that into a simple assignment,
> or whether there will be a call to delete() - or at least a branch to
> test if the internal member pointer is null.

I have removed that statement , removed called to rand, left 
only take, made int's non dynamic
and I got following time:

bmaxa@maxa:~/examples$ g++ -Wall -O2 -pthread -std=c++0x consprod1.cpp -o consprod1
consprod1.cpp: In lambda function:
consprod1.cpp:58:19: warning: unused variable ‘size’ [-Wunused-variable]
bmaxa@maxa:~/examples$ time ./consprod1

real	0m23.335s
user	0m22.177s
sys	0m16.417s

with java, I have removed rand too...
bmaxa@maxa:~/examples$ javac consprod.java
bmaxa@maxa:~/examples$ time java consprod

real	0m12.491s
user	0m21.001s
sys	0m0.524s

Still twice as fast as c++.
What it looks like, is that c++ spends much more time in syscalls
(futex) than java and that explains big difference.


Back to comp.programming.threads | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-22 23:59 +0200
  Re: Why is java consumer/producer so much faster than C++ Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-22 15:42 -0700
    Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 01:28 +0200
      Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-23 02:03 +0200
        Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 12:17 +0200
          Re: Why is java consumer/producer so much faster than C++ Luca Risolia <luca.risolia@studio.unibo.it> - 2012-07-24 00:33 +0200
  Re: Why is java consumer/producer so much faster than C++ Juha Nieminen <nospam@thanks.invalid> - 2012-07-23 06:37 +0000
    Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 12:33 +0200
      Re: Why is java consumer/producer so much faster than C++ Juha Nieminen <nospam@thanks.invalid> - 2012-07-23 11:46 +0000
        Re: Why is java consumer/producer so much faster than C++ Melzzzzz <mel@zzzzz.com> - 2012-07-23 15:33 +0200
  Re: Why is java consumer/producer so much faster than C++ Dombo <dombo@disposable.invalid> - 2012-07-23 22:57 +0200
    Re: Why is java consumer/producer so much faster than C++ Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-23 21:54 -0700
      Re: Why is java consumer/producer so much faster than C++ Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-07-24 12:50 +0000
  Re: Why is java consumer/producer so much faster than C++ Ricardo Nabinger Sanchez <rnsanchez@wait4.org> - 2012-08-14 22:48 +0000

csiph-web