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


Groups > comp.programming.threads > #956

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

From Melzzzzz <mel@zzzzz.com>
Newsgroups comp.lang.c++, comp.programming.threads
Subject Why is java consumer/producer so much faster than C++
Date 2012-07-22 23:59 +0200
Organization albasani.net
Message-ID <juht3o$a3a$1@news.albasani.net> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


I have played little bit with new C++11 features and compared,
java performance to c++.
Actually this was meant to be GC vs RAII memory management,
but boiled down to speed of BlockingQueue class in java,
and mine in c++.
It seems that java implementation is so much more efficient
but I don't know why. I even tried c++ without dynamic 
memory management (except queue itself) and that is *even slower*.
Must be some quirks with a queue ;)

These are timings:
(java openjdk 1.7)
bmaxa@maxa:~/examples$ time java consprod

real	0m13.411s
user	0m19.853s
sys	0m0.960s

(c++ gcc 4.6.3)
bmaxa@maxa:~/examples$ time ./consprod

real	0m28.726s
user	0m34.518s
sys	0m6.800s

Example programs follow (I think implementations of 
blocking queues are similar):
// java
import java.util.concurrent.*;
import java.util.Random;

class Vars{
public final static int nitems = 100000000;
public final static Random rnd = new Random(12345);
public final static int size = 10000;
}

class Producer implements Runnable {
   private final BlockingQueue<Integer> queue;
   Producer(BlockingQueue<Integer> q) { queue = q; }
   public void run() {
     try {
       int i = Vars.nitems;
       while(i-- > 0) { queue.put(produce(i)); }
     } catch (InterruptedException ex) 
     {
       
     }
   }
   Integer produce(int i) { return new Integer(i); }
 }

 class Consumer implements Runnable {
   private final BlockingQueue<Integer> queue;
   Consumer(BlockingQueue<Integer> q) 
   { 
    queue = q; 
   }
   public void run() {
     try {
       Integer[] arr = new Integer[10000];
       int i = Vars.nitems;
       while(i-- > 0) { consume(queue.take(),arr); }
     } catch (InterruptedException ex) 
     {
     }
   }
   void consume(Integer x, Integer[] arr) 
   {
    arr[Vars.rnd.nextInt(Vars.size)] = x;
   }
 }

 public class consprod {
   public static void main(String [] args) {
   try{
     BlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(100000);
     Producer p = new Producer(q);
     Consumer c = new Consumer(q);
     new Thread(p).start();
     new Thread(c).start();
     } catch(Exception e)
     {
      e.printStackTrace();
     }
   }
 }
//-----------------------------------------
// c++
#include <condition_variable>
#include <mutex>
#include <thread>
#include <deque>
#include <cstdlib>

template <class T>
class BlockingQueue{
public:
    BlockingQueue(unsigned cap):capacity_(cap)
    {
    }
    void put(T t)
    {
        std::unique_lock<std::mutex> lock(m_);
        while(queue_.size() >= capacity_)c_full_.wait(lock);
        queue_.push_back(std::move(t));
        c_empty_.notify_one();
    }
    T take()
    {
        std::unique_lock<std::mutex> lock(m_);
        while(queue_.empty())c_empty_.wait(lock);
        T tmp = std::move(queue_.front());
        queue_.pop_front();
        c_full_.notify_one();
        return std::move(tmp);
    }
    bool empty()
    {
        std::unique_lock<std::mutex> lock(m_);
        return queue_.empty();
    }
private:
    std::mutex m_;
    std::condition_variable c_empty_,c_full_;
    std::deque<T> queue_;
    unsigned capacity_;
};
 
int main()
{
    BlockingQueue<std::unique_ptr<int>> produced(100000);
    const int nitems = 100000000;
    std::srand(12345);
    

    std::function<void()> f_prod = [&]() {
        int i = nitems;
        while(i-- > 0){ 
            produced.put(std::unique_ptr<int>(new int(i)));
        }   
    };
    
    std::thread producer1(f_prod); 

    std::function<void()> f_cons = [&]() {
        const int size = 10000;
        std::unique_ptr<int> arr[size];
        int i = nitems;
        while(i-- > 0)
        {
            arr[std::rand()%size] = produced.take();
        }
    };
    
    std::thread consumer1(f_cons); 

    producer1.join();
    consumer1.join();
}
// --------------------------------------

Back to comp.programming.threads | Previous | NextNext 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