Performance dmd vs ldc2

John Colvin john.loughran.colvin at gmail.com
Tue Jul 30 09:43:50 PDT 2013


On Tuesday, 30 July 2013 at 09:21:38 UTC, Chris wrote:
> On Tuesday, 30 July 2013 at 09:10:24 UTC, Temtaime wrote:
>> I have a little question..
>> Why Digital Mars doesn't uses LLVM for backend and develops 
>> their own performance killer ?
>> Is someone contact with them to drop out their backend ?
>
> I checked it once more to make sure my code is not the problem. 
> I used the xml parsing example on this page 
> http://dlang.org/phobos/std_xml.html (the second example). I 
> created a dummy "books.xml" file with one entry (see below for 
> source). The result:
>
> Loop: 0..100
>
> DMD 2.063
> dmd -release -noboundscheck -O -inline
> Average time: 41.88 msecs
>
> LDC2 (latest version, no flags)
> ldc2 [files]
> Average time: 0.01 msecs
>
> ------ D Code -------
>
> import std.xml;
> import std.stdio;
> import std.string;
> import std.datetime;
>
> void main() {
> 	StopWatch sw;
>   enum n = 100;
>   TickDuration[n] times;
>   TickDuration last = TickDuration.from!"seconds"(0);
>
>   foreach (i; 0..n) {
>   	sw.start();
>   	parse();
>   	//writeln("Time ", sw.peek().msecs, " [ms]");
>   	sw.stop();
>   	times[i] = sw.peek() - last;
>     last = sw.peek();
>   }
>    real sum = 0;
>    // To know the number of seconds,
>     // use properties of TickDuration.
>     // (seconds, msecs, usecs, hnsecs)
>    foreach(t; times) {
>    	sum += t.msecs;
>    }
>    writeln("Average time: ", sum/n, " msecs");
> }
>
> struct Book
> {
>     string id;
>     string author;
>     string title;
>     string genre;
>     string price;
>     string pubDate;
>     string description;
> }
>
> void parse() {
> 	string s = cast(string)std.file.read("books.xml");
>
>     // Check for well-formedness
>     check(s);
>
>     // Take it apart
>     Book[] books;
>
>     auto xml = new DocumentParser(s);
>     xml.onStartTag["book"] = (ElementParser xml)
>     {
>         Book book;
>         book.id = xml.tag.attr["id"];
> 				
>         xml.onEndTag["author"]       = (in Element e) { 
> book.author      = e.text(); };
>         xml.onEndTag["title"]        = (in Element e) { 
> book.title       = e.text(); };
>         xml.onEndTag["genre"]        = (in Element e) { 
> book.genre       = e.text(); };
>         xml.onEndTag["price"]        = (in Element e) { 
> book.price       = e.text(); };
>         xml.onEndTag["publish-date"] = (in Element e) { 
> book.pubDate     = e.text(); };
>         xml.onEndTag["description"]  = (in Element e) { 
> book.description = e.text(); };
>
>         xml.parse();
>
>         books ~= book;
>     };
>     xml.parse();
>
>     // Put it back together again;
>     auto doc = new Document(new Tag("catalog"));
>     foreach(book;books)
>     {
>         auto element = new Element("book");
>         element.tag.attr["id"] = book.id;
>
>         element ~= new Element("author",      book.author);
>         element ~= new Element("title",       book.title);
>         element ~= new Element("genre",       book.genre);
>         element ~= new Element("price",       book.price);
>         element ~= new Element("publish-date",book.pubDate);
>         element ~= new Element("description", book.description);
>
>         doc ~= element;
>     }
>
>     // Pretty-print it
>     //writefln(join(doc.pretty(3),"\n"));
> }
>
> ------ XML FILE ------
>
> <?xml version="1.0" encoding="UTF-8"?>
> <books>
> 	<book id="1-23456789">
> 		<author>Count Dracula</author>
> 		<title>D for Vampires</title>
> 		<genre>Programming</genre>
> 		<price>66.6</price>
> 		<publish-date>1897</publish-date>
> 		<description>Tomb programming with D</description>
> 	</book>
> </books>

Are you sure that ldc hasn't actually just optimised away the 
whole of parse()?
Check to asm to be sure.


More information about the digitalmars-d-ldc mailing list