www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - We can see the performance difference from the simple functions in Tango and Phobos

reply zsxxsz <zhengshuxin hexun.com> writes:
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,"&","&amp;"); if (s.indexOf('"') != -1) s = replace(s,"\"","&quot;"); if (s.indexOf("'") != -1) s = replace(s,"'","&apos;"); 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. :)
Jul 30 2009
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
zsxxsz wrote:
 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,"&","&amp;"); if (s.indexOf('"') != -1) s = replace(s,"\"","&quot;"); if (s.indexOf("'") != -1) s = replace(s,"'","&apos;"); if (s.indexOf('<') != -1) s = replace(s,"<","&lt;"); if (s.indexOf('>') != -1) s = replace(s,">","&gt;"); return s; }
That's from std.xml. I won't make any excuse for it, it's very inefficient.
Jul 30 2009
prev sibling next sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Thu, Jul 30, 2009 at 9:35 PM, zsxxsz<zhengshuxin hexun.com> wrote:
 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.
Uh, who exactly are you trying to convince? For that matter, what's the point of this thread?
Jul 30 2009
parent zsxxsz <zhengshuxin hexun.com> writes:
== Quote from Jarrett Billingsley (jarrett.billingsley gmail.com)'s article
 On Thu, Jul 30, 2009 at 9:35 PM, zsxxsz<zhengshuxin hexun.com> wrote:
 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.
Uh, who exactly are you trying to convince? For that matter, what's the point of this thread?
I like D very much, although I'm a C programmer. Phobos, as the stdlib of D, not only have the good code style, but also give the better performance and more good characters. Tango maybe one substitute, but it make us confused there are two basic library exist, if you use one, you can't use the other one, how painful it is. Why can't it be integrated into one? The two libs do have its own good characters.
Jul 30 2009
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
zsxxsz wrote:
 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.
[snip]
 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. :)
That code is terrible, thanks for pointing it out to me. I posted a bug report on your behalf: http://d.puremagic.com/issues/show_bug.cgi?id=3218 (I'm unhappy with both functions because neither works with on ranges and neither accepts all three character widths.) Any similar findings would be very appreciated. Andrei
Jul 30 2009