Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL algorithm VS Java loop (http://www.velocityreviews.com/forums/t451151-stl-algorithm-vs-java-loop.html)

 Allerdyce.John@gmail.com 01-17-2006 05:25 PM

STL algorithm VS Java loop

Hi,

I am trying to compare the amount of work between using STL algorithm
VS a plain Java loop.

Let's say the class Rect has 2 attributes: area, and areaPerCent.

In Java, I just write a plain for loop with a list:
public static void calculateAreaPerCent(List rectList, float
containerArea) {
for (Iterator iter = rectList.iterator(); iter.hasNext();) {
Rect r = (Rect) iter.next();
r.areaPerCent = r.area / containerArea;
}
}

And in C++, it is recommded to use STL algorithm instead of a plain
loop, we need to do this:
class doloop : public unary_function<Rect*, void>
{
public:
loopthru(float containerArea) : _containerArea(containerArea) { }

void operator() (Rect* r) {

r->areaPerCent = r->area /_containerArea;
}
private:
_containerArea;

};

void calculateAreaPerCent(vector<Rect*>& rl, float containerArea) {
for_each (rl.begin(), rl.end(), doloop(containerArea));
}

It seems to me the C++ STL algorithm way needs more work. Is that a
fair comparision? Is there a simpler way? Or I should use a plain C++
for loop ?

Thank you.

 Axter 01-17-2006 05:45 PM

Re: STL algorithm VS Java loop

Allerdyce.John@gmail.com wrote:
> Hi,
>
> I am trying to compare the amount of work between using STL algorithm
> VS a plain Java loop.
>
> Let's say the class Rect has 2 attributes: area, and areaPerCent.
>
> In Java, I just write a plain for loop with a list:
> public static void calculateAreaPerCent(List rectList, float
> containerArea) {
> for (Iterator iter = rectList.iterator(); iter.hasNext();) {
> Rect r = (Rect) iter.next();
> r.areaPerCent = r.area / containerArea;
> }
> }
>
> And in C++, it is recommded to use STL algorithm instead of a plain
> loop, we need to do this:
> class doloop : public unary_function<Rect*, void>
> {
> public:
> loopthru(float containerArea) : _containerArea(containerArea) { }
>
> void operator() (Rect* r) {
>
> r->areaPerCent = r->area /_containerArea;
> }
> private:
> _containerArea;
>
> };
>
> void calculateAreaPerCent(vector<Rect*>& rl, float containerArea) {
> for_each (rl.begin(), rl.end(), doloop(containerArea));
> }

You don't need to use a for_each in C++, and you can use the common
for() loop method.
Use the fore_each method when you have a generic function, that can be
used with multiple types.
Java doesn't have this option, so it's harder to reuse code in Java.
In C++ we have more options, and therefore can do it in a manner that
is best for a paticular application.

>
> It seems to me the C++ STL algorithm way needs more work. Is that a
> fair comparision?

No. Since you're comparring apples and oranges. You can use nearly
the same syntax used in Java, and you have more efficient code.

> Is there a simpler way? Or I should use a plain C++ for loop ?

You should be using the plain C++ loop, unless you have a generic
function.

Moreover, you should avoid using pointers unless you're sure you have
to.
In Java, everything is basically a pointer.
In C++, you don't need to use pointers, unless you're using abstract
types.

If Rect is not an abstract type, than there's no need to use a vector
of pointers.
vector<Rect> vRect;

If Rect is an abstract type, than you should use a smart pointer,
instead of a raw dummy pointer.
vector<boost::shared_ptr<Rect> > vRect;
or
vector<cow_ptr<Rect> > vRect;

See following link for cow pointer:
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h

And check out the boost library for shared_ptr.

 Markus Moll 01-17-2006 06:07 PM

Re: STL algorithm VS Java loop

Hi

Axter wrote:

> You don't need to use a for_each in C++, and you can use the common
> for() loop method.
> Use the fore_each method when you have a generic function, that can be
> used with multiple types.

Why?
I don't see any relation...

> Java doesn't have this option, so it's harder to reuse code in Java.

Java 5 knows generics, btw...

>> It seems to me the C++ STL algorithm way needs more work. Is that a
>> fair comparision?

>
> No. Since you're comparring apples and oranges. You can use nearly
> the same syntax used in Java, and you have more efficient code.

More efficient? I doubt that for_each(begin,end,action) boils down to
anything else but for(For i=begin; i!=end; ++i) action(*i);

Markus

 Alan Johnson 01-17-2006 07:25 PM

Re: STL algorithm VS Java loop

Axter wrote:
> vector<boost::shared_ptr<Rect> > vRect;

Boost also has a "pointer container" library that you might find more
suitable than a vector of shared_ptr, depending on how much flexibility

http://www.boost.org/libs/ptr_contai...container.html

-Alan

 Axter 01-17-2006 07:58 PM

Re: STL algorithm VS Java loop

Markus Moll wrote:
> Hi
>
> Axter wrote:
>
> > You don't need to use a for_each in C++, and you can use the common
> > for() loop method.
> > Use the fore_each method when you have a generic function, that can be
> > used with multiple types.

>
> Why?
> I don't see any relation...
>
> > Java doesn't have this option, so it's harder to reuse code in Java.

>
> Java 5 knows generics, btw...

Generic what?
Can you do template coding with Java 5?

> >> It seems to me the C++ STL algorithm way needs more work. Is that a
> >> fair comparision?

> >
> > No. Since you're comparring apples and oranges. You can use nearly
> > the same syntax used in Java, and you have more efficient code.

>
> More efficient? I doubt that for_each(begin,end,action) boils down to
> anything else but for(For i=begin; i!=end; ++i) action(*i);

Have you tried using a Java IDE?
Every Java IDE I've tried is painfully slow.
IMHO, why should I bother using Java, if a Java developer can't make a
decent efficient IDE for the language?
By evaluating the performance of most Java IDE's, I have to assume
either there are a lot of bad Java programmers developing IDE's, or
the language is inefficient in general.

 Markus Moll 01-17-2006 09:48 PM

Re: STL algorithm VS Java loop

Hi

Axter wrote:

> Generic what?
> Can you do template coding with Java 5?

Depends on what your understanding of "template coding" is.
You certainly cannot do everything you could do with templates.
See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf if you want to
know more.

But we're moving off topic...

>> More efficient? I doubt that for_each(begin,end,action) boils down to
>> anything else but for(For i=begin; i!=end; ++i) action(*i);

>
> Have you tried using a Java IDE?

Eclipse runs quite well.

But anyway, it was a misunderstanding. I thought you were comparing for_each
to a simple for loop, not Java to C++.

Markus

 Allerdyce.John@gmail.com 01-17-2006 09:55 PM

Re: STL algorithm VS Java loop

I thought it is better to use for_each than a plain c++ for loop. That
is why I post my original question.

>From Effective STL item 43: Prefer algorithm calls to hand-written

loops.

Otherwise, there is no need to have the for_each() algorithm, right? we
can replace all for_each() calls with a plain c++ for loop.

Thank you.

 eiji 01-17-2006 09:58 PM

Re: STL algorithm VS Java loop

In nearly every second link the guy's in comp.lang.c++ are trying to
dispose the shared pointers!

So my question:

#####some example from boost-hp#####
ptr_vector<X> pvec;
std::vector<X*> vec;
*vec.begin() = new X; // fine, memory leak
*pvec.begin() = new X; // compile time error
( *vec.begin() )->foo(); // call X::foo(), a bit clumsy
pvec.begin()->foo(); // no indirection needed
*vec.front() = X(); // overwrite first element
pvec.front() = X(); // no indirection needed

#####So this is what I first think of that:
ptr_vector<X*> pvec; //for generics!!
std::vector<X*> vec;
*vec.begin() = new X; // just be sure to delete it, where is the
problem?
*pvec.begin() = new X; // now no error!
( *vec.begin() )->foo(); // call X::foo(), a bit clumsy, but correct
pvec.begin()->foo(); // no indirection needed, yes but no generics
*vec.front() = X(); // overwrite first element
pvec.front() = X(); // compile time error

########
Is this possible?

pvec vec
*vec.begin() = new X;
( *vec.begin() )->foo(); // call X::foo(), a bit clumsy, but correct
*vec.front() = X(); // overwrite first element
delete *vec.begin(); // delete first element

does that compile with either
typedef std::vector<X*> pvec
or
typedef ptr_vector<X*> pvec

Regards,
Sascha

 Luke Meyers 01-18-2006 08:23 AM

Re: STL algorithm VS Java loop

Markus Moll wrote:
> >> It seems to me the C++ STL algorithm way needs more work. Is that a
> >> fair comparision?

> >
> > No. Since you're comparring apples and oranges. You can use nearly
> > the same syntax used in Java, and you have more efficient code.

>
> More efficient? I doubt that for_each(begin,end,action) boils down to
> anything else but for(For i=begin; i!=end; ++i) action(*i);

I think you might be surprised. Ever heard of loop unrolling, for
instance?

Or, better yet, measure it yourself. It's really not hard to get some
basic perf numbers and answer these things empirically. Or go look up
the source of your favorite STL implementation.

Java has all kinds of difficulty competing in terms of performance.
For example, I recently found a case in which javac generates an extra
bytecode if certain parentheses are added for clarity.

Luke

 Allerdyce.John@gmail.com 01-18-2006 04:43 PM

Re: STL algorithm VS Java loop

I guess I was trying to find out what are the advantages of using STL
algorithms over a plain C++ for loop. I did not mean it to be a Java
Vs C++ discussion.

In my provided example, it seems to me it requires more code to use STL
algorithms over a plain c++ loop. And yet, in item 43 of Effective STL
, it said 'perfer algorithm calls to hand-written loops'. And this is
what I don't understand...

Thank you.

All times are GMT. The time now is 05:09 PM.