[Rosettacode] Growable slices

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 16 10:41:02 PDT 2014


On 05/16/14 17:20, bearophile via Digitalmars-d-learn wrote:
> Artur Skawina:
> 
>> Ugh. So how does it perform wrt the D version that I wrote for
>> you last time? [1]
> 
> I have done a benchmark with the various version (the first 3 are the ones on the Rosettacode site, and the #4 is yours):
> 
> lzw1: 0.39
> lzw2: 0.17
> lzw3: 0.21
> lzw4: 0.17
> 
> I think your comment was about an older version of the first entry, that is non meant to be fast, just short and simple.

While I don't remember the numbers, I did test w/ gdc back then and
both of your versions (from that thread) performed very poorly in
a micro benchmark - I wasn't exaggerating when I said /an order
of magnitude/, there really was a ~10 times perf difference.
I didn't read the current entries on that page, so that might no
longer apply, if you've modified them since.

My point is that this third version that you announced here as almost-
idiomatic-D is not even close to that goal; it's more or less a direct
translation from C, and not like anything that would be written from
scratch in D, hence the "ugh". Your own numbers above suggest that 
not even the "more efficient" claim is true. 

> I think the second version is enough. Do you agree?

I don't see the point of the third ("C-in-D") one, other than to
show how a direct C to D translation would look like.

If /I/ was looking for a D code sample, then
  
  OUT[] compress(OUT=int, R)(R original) {
     IDAA!(OUT, const typeof(cast()original.front)[]) dictionary;
     OUT[] result;
     size_t l = 1;

     while (original.length>=l) {
        if (original.takeExactly(l) in dictionary)
           { ++l; continue; }
        result ~= dictionary[original.takeExactly(l-1)];
        dictionary[original.takeExactly(l)] = cast(OUT)dictionary.length;
        original.popFrontN(l-1);
        l = 1;
     }

     return original.empty ? result : (result ~ dictionary[original]);
  }

would have been much more useful than any of the currently available
examples... Your #1 is more readable and easier to understand for
someone not familiar with D and ranges, but extremely inefficient
and not something that anyone would like to use in real life.

Many of the rosettacode entries are very misleading and unidiomatic,
they give a very wrong picture of the language they're trying to
show off. This is probably not specific to D, but true for most
of the represented languages.

artur


More information about the Digitalmars-d-learn mailing list