We can see the performance difference from the simple functions in Tango and Phobos

zsxxsz zhengshuxin at hexun.com
Thu Jul 30 18:35:32 PDT 2009


Hi, below are the functions from Phobos and Tango with the same use, we can
see why so many people like Tango more than Phobos.

>>> In Phobos:

string encode(string s)
{
    // The ifs are (temprarily, we hope) necessary, because
    // std.string.write.replace
    // does not do copy-on-write, but instead copies always.

    if (s.indexOf('&') != -1) s = replace(s,"&","&");
    if (s.indexOf('"') != -1) s = replace(s,"\"",""");
    if (s.indexOf("'") != -1) s = replace(s,"'","'");
    if (s.indexOf('<') != -1) s = replace(s,"<","&lt;");
    if (s.indexOf('>') != -1) s = replace(s,">","&gt;");
    return s;
}

>>>In Tango:

T[] toEntity(T) (T[] src, T[] dst = null)
{
        T[]  entity;
        auto s = src.ptr;
        auto t = s;
        auto e = s + src.length;
        auto index = 0;

        while (s < e)
               switch (*s)
                      {
                      case '"':
                           entity = "&quot;";
                           goto common;

                      case '>':
                           entity = "&gt;";
                           goto common;

                      case '<':
                           entity = "&lt;";
                           goto common;

                      case '&':
                           entity = "&amp;";
                           goto common;

                      case '\'':
                           entity = "&apos;";
                           goto common;

                      common:
                           auto len = s - t;
                           if (dst.length <= index + len + entity.length)
                               dst.length = (dst.length + len + entity.length)
+ dst.length / 2;

                           dst [index .. index + len] = t [0 .. len];
                           index += len;

                           dst [index .. index + entity.length] = entity;
                           index += entity.length;
                           t = ++s;
                           break;

                      default:
                           ++s;
                           break;
                      }


        // did we change anything?
        if (index)
           {
           // copy tail too
           auto len = e - t;
           if (dst.length <= index + len)
               dst.length = index + len;

           dst [index .. index + len] = t [0 .. len];
           return dst [0 .. index + len];
           }

        return src;
}

We can see the function's performance from Tango is more high than which one
from Phobos. This maybe not the only one function difference. :)



More information about the Digitalmars-d mailing list