On Monday, July 23, 2012 6:32:10 PM UTC-4, Melzzzzz wrote:
On Mon, 23 Jul 2012 13:23:30 -0700 (PDT)
Howard Hinnant wrote:
> I made a few changes to your code:
>
>
> On my system this sped the C++ solution up considerably (to
about > 13.7 seconds). I didn't time the Java solution.
Yes, that's it, on my system:
bmaxa@maxa:~/examples$ time ./consprod2
real 0m9.478s
user 0m10.797s
sys 0m7.196s
which is more than twice speed of original program!
(and faster than java)
At second glance I wasn't that fond of my solution and tweaked it as
below:
void put(T t)
{
std::unique_lock<std::mutex> lock(m_, std::try_to_lock);
while (!lock.owns_lock())
{
if (queue_.size() > capacity_/4)
{
for (int i = 0; i < 3250; ++i)
std::this_thread::yield();
lock.try_lock();
}
else
{
lock.lock();
break;
}
}
while(queue_.size() >= capacity_)c_full_.wait(lock);
queue_.push_back(std::move(t));
if (queue_.size() == 1)
c_empty_.notify_one();
}
T take()
{
std::unique_lock<std::mutex> lock(m_, std::try_to_lock);
int retry = 0;
while (!lock.owns_lock())
{
if (queue_.size() < 3*capacity_/4)
{
for (int i = 0; i < 3250; ++i)
std::this_thread::yield();
lock.try_lock();
}
else
{
lock.lock();
break;
}
}
while(queue_.empty())c_empty_.wait(lock);
T tmp = std::move(queue_.front());
queue_.pop_front();
if (queue_.size() == capacity_-1)
c_full_.notify_one();
return tmp;
}
I am admittedly coding to the benchmark (which is not a really great
idea). But I got the time on my system down from 13.7 seconds to
about 9.7 seconds (another 40%).
On my system speed up is not that big.