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


Groups > comp.programming.threads > #967

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

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 2012-07-23 12:33 +0200
Organization albasani.net
Message-ID <juj99h$ges$2@news.albasani.net> (permalink)
References <juht3o$a3a$1@news.albasani.net> <juiret$jqb$1@speranza.aioe.org>

Cross-posted to 2 groups.

Show all headers | View raw


On Mon, 23 Jul 2012 06:37:17 +0000 (UTC)
Juha Nieminen <nospam@thanks.invalid> wrote:

> In comp.lang.c++ Melzzzzz <mel@zzzzz.com> wrote:
> > I even tried c++ without dynamic 
> > memory management (except queue itself) and that is *even slower*.
> 
> If two C++ programs are otherwise identical, except that one uses
> 'new int' and the other just uses the ints by value, and the former
> is faster than the latter, then there's something horribly wrong in
> the way you are measuring, or something else.

I think that pressure on condition variable is greater with non
dynamic allocation, therefore it is slower.
If I reduce queue capacity on eg 1000 (more pressure on condition
variable) than it is much slower than with capacity of 100000.


 I don't think it's
> physically possible for 'new int' to be faster than using ints by
> value under any possible circumstance, even if we assumed a highly
> optimized version of 'new' that does magic under the hood to be 10
> times faster than the regular 'new'.

That's because new is very fast and don;t take much time of program
in this example.

> 
> >        std::unique_lock<std::mutex> lock(m_);
> 
> >            produced.put(std::unique_ptr<int>(new int(i)));
> 
> >            arr[std::rand()%size] = produced.take();
> 
> I don't know what's the cause of the slowness in your program, but
> I quoted above the three possible culprits I would first investigate
> (which happen to be in order of likeliness).
> 
> Locks are slow. They are probably slower than 'new'.

I think that locks are not that slow in comparison to
condition variables. I have feeling that somehow
Java has less pressure on condition variables
in it's queue implementation.

> 
> 'new' is slow, especially when compared to java's.

In this example new takes very little time.

> 
> std::rand() is slow, although probably does not account for that much
> slowness, but could be a minor contributing factor.

This also takes little time.
Here is difference between new and rand and without.
Both versions are optimized now as per Luca's suggestion.


(with new and rand array assignment)
bmaxa@maxa:~/examples$ time ./consprod

real	0m25.127s
user	0m32.202s
sys	0m5.540s
(without)
bmaxa@maxa:~/examples$ time ./consprod1

real	0m23.883s
user	0m23.009s
sys	0m17.153s

Java:
(without rand and array assignment)
bmaxa@maxa:~/examples$ time java consprod

real	0m12.825s
user	0m21.833s
sys	0m0.592s

(with rand and array assignment)
bmaxa@maxa:~/examples$ time java consprod

real	0m15.571s
user	0m25.634s
sys	0m1.168s

Java pays higher price for random assignments, I think.

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