I didn’t test timings, but the C++/Java comparisons I did were not
exactly fair. I knew the C++ could compile more efficiently. At
least
without clever optimization.
C++, *imageIt = lookup[*imageIt]; imageIt++
Change that to ++imageIt and you may get a speed improvement on some
compilers (++imageIt has to create a temporary copy of the iterator,
increment the iterator, and return the copy).
Java, image[i] = lookup[image[i]]; i++;
I also recognize that Ruby is interpreted. However, I did not expect
the factor of 200 difference. The interpreter only needs to parse the
loop once, it should then have byte code to execute for each
iteration.
The Ruby interpreter does not presently use byte code; it traverses a
tree that represents your program.
I knew Ruby was not up for my current task. That is why it is being
written in C++. I was just curious at close it could come. I was
really blown away, by exactly how inadequate it is at such things.
As some people have already suggested, the development model that seems
to work best is to write it in Ruby, profile it, then rewrite the parts
that are too slow as a C extension.
However, Ruby blocks are really nice. I have been trying to make my
C++ do the same thing and I just end up with this mess.
template
void map(FuncData &data, T &v)
{
v = data.lookup[v];
}
template
void for_each_pixel(ImageData &id, FuncData &fd, void
(*func)(FuncDataOnly &fdo, T &v) )
{
T *imageIt = id.image;
T *pLast = imageIt + id.imageSize;
while (imageIt != pLast)
{
(*func)(fd, *imageIt);
imageIt++;
}
}
You shouldn’t have to write your own for_each_pixel; the C++ standard
library already has a for_each function, thus you can write:
template
struct map {
map(FuncData & data) : data_(data) { }
void operator(T & v) { v = data.lookup[v]; }
FuncData const & data_;
};
std::for_each(id.image, id.image + id.imageSize, map(fd));
Or you could instead use std::transform (see
Cash Advance Loans, Instant Decisions | £100 to £5000 in 15 Mins).
You can also get block-like effects with template tricks. This is what
the lambda library and phoenix do:
Chapter 18. Boost.Lambda - 1.84.0
The Spirit Parser Library download | SourceForge.net
Neither is as neat or clean as Ruby’s blocks, but they are fast, are
generated at compile-time, and they are all you’ve got if you want
lambda functions in C++.
Paul
···
On Fri, Nov 22, 2002 at 03:56:18AM +0900, MetalOne wrote: