tag:blogger.com,1999:blog-6819665048152280962024-02-07T01:26:47.320-08:00nino porcinopersonal blogAntonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-681966504815228096.post-68793400041152705622014-08-28T00:36:00.002-07:002014-08-28T00:36:57.903-07:00Java GWT added to Dart RayTracer demoThe <a href="https://github.com/nippur72/Dart.RayTracerDemo">Dart RayTracer</a> demo is getting bigger!<br />
<br />
Thanks to <a href="https://github.com/timeu">Ümit Seren</a> and <a href="https://github.com/slavap">Slava Pankov</a>, we now have a Java port that compiles both for the native JVM and the browser (via <a href="http://www.gwtproject.org/">GWT </a>compiler).<br />
<br />
Here are the updated test results with the inclusion of Java:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJvN4GlSv9dYkKExRwYLgxijTxpmvMLNv9evvIV1wWBX8zJHiV2uEzhXVOCjX2G9KTVzssYnu3cxSt5_1oC6WFvbGlkIWBC-J8RXSe33hd0MUQiHzw6M0Y3nvnNaJxQSgYTOhC6MQKJ2c/s1600/table.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJvN4GlSv9dYkKExRwYLgxijTxpmvMLNv9evvIV1wWBX8zJHiV2uEzhXVOCjX2G9KTVzssYnu3cxSt5_1oC6WFvbGlkIWBC-J8RXSe33hd0MUQiHzw6M0Y3nvnNaJxQSgYTOhC6MQKJ2c/s1600/table.png" height="318" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmH3fXI1xbe-IhNhFxjDYtdjc8JgGfjY4TgvtYPt2hjKZq5M8gU6hy5ukDd6RPuKaWBkctvtkKPvC4o9oaxPwF_ap7MYsP9UzCNWeMjjfwxrdG_g8YUFXLHMtblV96Lwtk0Tiah2uTTI8/s1600/graph.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmH3fXI1xbe-IhNhFxjDYtdjc8JgGfjY4TgvtYPt2hjKZq5M8gU6hy5ukDd6RPuKaWBkctvtkKPvC4o9oaxPwF_ap7MYsP9UzCNWeMjjfwxrdG_g8YUFXLHMtblV96Lwtk0Tiah2uTTI8/s1600/graph.png" height="408" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
Java's JVM jumps in first position resulting the fastest among all. Java on the browser via GWT does a pretty well job too, rivaling its direct competitor (C# to JS).<br />
<br />
Disappointing results from C# native CLR, which resulted the slowest virtual machine of the set, not even able to reach the performance of JavaScript transpiled C# (running on Chrome).<br />
<br />
<br />
Get this test from <a href="https://github.com/nippur72/Dart.RayTracerDemo">GitHub </a>or see my previous <a href="http://ninoporcino.blogspot.it/2014/08/dart-ray-tracer-benchmark-compared-to-c.html">blog post</a> about it.<br />
<br />Antonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.com0tag:blogger.com,1999:blog-681966504815228096.post-4491386808874143122014-08-22T10:58:00.000-07:002014-08-25T08:47:47.473-07:00Dart ray tracer benchmark (compared to C#)As an exercise in Dart programming, this morning I've ported <a href="https://github.com/nippur72/Saltarelle.RaytracerDemo">this Raytracer demo</a> from C# to Dart.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe6RMsnu5u5Wg6oarsN12Ls4zWtG42Qkcyi_isPVoFk_05hyphenhyphenqQPxhKqpKTWrxEV0CjE5S9iN5ILB4FoXjxAOvZQrOH3Zn9msBsRMwrO9MNGYfwhyD6lZs94x1ryq3gn7dQs1dGo63pjXc/s1600/canvas.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhe6RMsnu5u5Wg6oarsN12Ls4zWtG42Qkcyi_isPVoFk_05hyphenhyphenqQPxhKqpKTWrxEV0CjE5S9iN5ILB4FoXjxAOvZQrOH3Zn9msBsRMwrO9MNGYfwhyD6lZs94x1ryq3gn7dQs1dGo63pjXc/s1600/canvas.png" height="240" width="320" /></a></div>
<br />
You can get it from <a href="https://github.com/nippur72/Dart.RayTracerDemo">Github.</a><br />
<br />
Two years ago, I've used the same raytracer program to compare the performances of two C#-to-JavaScript compilers, <a href="http://jsil.org/">JSIL</a> and <a href="http://www.saltarelle-compiler.com/">Saltarelle</a> (see my <a href="http://ninoporcino.blogspot.it/2012/12/saltarelle-vs-jsil-raytracer-demo.html">blogpost</a>).<br />
<br />
Now I use the same program to compare C# and <a href="http://dartlang.org/">Dart</a>.<br />
<br />
Speed comparison is a marginal topic here (though it will be curious to see who's fastest in rendering the raytraced scene), I am more interested in the overall programming experience as this is my first attempt at Dart (apart the classic "hello world"). In brief, I have to decide if Dart it's worth embracing, perhaps for a future replacement of C#, considering that Microsoft is not pushing it for client-side web development (they are investing in <a href="http://www.typescriptlang.org/">TypeScript</a> instead).<br />
<h2>
Porting the raytracer demo</h2>
So I download the <a href="http://dartlang.org/">64 bit windows version</a> of the Dart editor and decompress in my desktop. The editor doesn't need to be installed, it just runs from the directory. Good.<br />
<br />
The editor is a stripped version of Eclipse, the same IDE it's used for Java programming. For a long term Visual Studio user, it's a rather chaotic, inconsistent and ugly-looking IDE, and I have to force myself to get accustomed to it. My locale (italian) apparently is not available (as any other locales), so I have to stick to English. Not a good first impression. <br />
<br />
While editing the code, the IDE flickers a lot and lags in intellisense/syntax highlighting. Sometimes typing "<span style="font-family: "Courier New",Courier,monospace;">.</span>" or "<span style="font-family: "Courier New",Courier,monospace;">(</span>" does not produce the intellisense dialog (don't know why!). And if the source code has too many errors, the whole syntax correction is unstable and unreliable, for instance for a while it gave me a clueless "<span style="font-family: "Courier New",Courier,monospace;">int is not a type</span>", making me wonder if I had correctly imported the libraries. <br />
<br />
Anyway at the end I got used to the IDE and learned to cope with its defects.<br />
<br />
For the raytracer demo, I started a new project, and copied the <span style="font-family: "Courier New",Courier,monospace;">.html</span> and <span style="font-family: "Courier New",Courier,monospace;">.cs</span> files from the old C# project. I then renamed to <span style="font-family: "Courier New",Courier,monospace;">.dart</span> and started to convert it all. The whole process, from installing Dart to seeing the raytraced image, took about three hours. I was afraid of getting stuck somewhere, but to my luck that didn't happen. Fear of the unknown I suppose. <br />
<br />
Knowing very little of Dart, I decided to resort to Google for everything, even for the most trivial problem. Dart documentation is scarce, but a trick I've learned is that if you "<span style="font-family: "Courier New",Courier,monospace;">open declaration</span>" of a method, you are taken directly to the source code and can see directly the API comments and/or peek around. Sometimes this is quicker than goggling. <br />
<br />
A page that helped me a lot is <a href="https://www.dartlang.org/docs/synonyms/#theme-html-attributes">this comparison</a> between languages, for those who already know another mainstream language. It bootstraps you in very short time. <br />
<br />
Back to the code. The first thing I do is to remove by find'n'replace all occurrences of "<span style="font-family: "Courier New",Courier,monospace;">public</span>" because in Dart everything is public by default. Good, C# is too verbose in this regard. In my code there are also some "<span style="font-family: "Courier New",Courier,monospace;">private</span>" variables that I rename by prefixing them with "<span style="font-family: "Courier New",Courier,monospace;">_</span>". That's how Dart marks privates. Don't like much this design, but I comprehend where it comes from. <br />
<br />
Another easy replace is "<span style="font-family: "Courier New",Courier,monospace;">extends</span>" in place of "<span style="font-family: "Courier New",Courier,monospace;">:</span>" for class inheritance.<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">float </span>data type is not available in Dart so I replace it with <span style="font-family: "Courier New",Courier,monospace;">double</span>, removing also the "<span style="font-family: "Courier New",Courier,monospace;">f</span>" from numeric constants (like <span style="font-family: "Courier New",Courier,monospace;">3.1415f</span>). It doesn't matter much because even in C# floats are mapped to the JavaScript <span style="font-family: "Courier New",Courier,monospace;">number </span>data type.<br />
<br />
Looking at the docs, I find that <span style="font-family: "Courier New",Courier,monospace;">Double.MaxValue</span> and <span style="font-family: "Courier New",Courier,monospace;">Double.MinValue</span> are different, in Dart they are: <span style="font-family: "Courier New",Courier,monospace;">double.MAX_FINITE</span> and <span style="font-family: "Courier New",Courier,monospace;">double.MIN_POSITIVE</span>.<br />
<br />
Dart has no namespaces (it has libraries instead), so I completely remove them from the code. <br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">import </span>takes the place of <span style="font-family: "Courier New",Courier,monospace;">using </span>in Dart, with a little difference. You import files instead of namespaces. So for instance if your projects has three <span style="font-family: "Courier New",Courier,monospace;">.dart</span> files, you have to <span style="font-family: "Courier New",Courier,monospace;">import </span>them where needed. In C#, once the files are added to the project, there is no need to declare them, unless they are on a different namespace (in that case you use <span style="font-family: "Courier New",Courier,monospace;">using</span>). Dart approach is more dynamic, and clearly reminds of the Javascript approach (and honestly I think that makes sense in the context of a dynamic language). <br />
<br />
Overall, I had to import the following libraries<br />
<ul>
<li>"<span style="font-family: "Courier New",Courier,monospace;">dart:html</span>" to be able to work with the canvas (the raytraced image) </li>
<li>"<span style="font-family: "Courier New",Courier,monospace;">dart:js</span>" for JavaScript interop (see later)</li>
<li>"<span style="font-family: "Courier New",Courier,monospace;">dart:math</span>" for <span style="font-family: "Courier New",Courier,monospace;">sqrt()</span> (in place of <span style="font-family: "Courier New",Courier,monospace;">Math.Sqrt</span>). <span style="font-family: "Courier New",Courier,monospace;">Math.Floor()</span> is done via <span style="font-family: "Courier New",Courier,monospace;">double.floor()</span>.</li>
</ul>
To my surprise Dart has no cast (e.g. <span style="font-family: "Courier New",Courier,monospace;">(double)x</span>), and in a certain sense it's less tolerant than C#. For example you can't pass "<span style="font-family: "Courier New",Courier,monospace;">0</span>" or "<span style="font-family: "Courier New",Courier,monospace;">1</span>" as a <span style="font-family: "Courier New",Courier,monospace;">double </span>parameter, but you have to be specific by writing "<span style="font-family: "Courier New",Courier,monospace;">0.0</span>" and "<span style="font-family: "Courier New",Courier,monospace;">1.0</span>". A bit annoying. Also I had to specify the <span style="font-family: "Courier New",Courier,monospace;">~/</span> operator when dividing by an <span style="font-family: "Courier New",Courier,monospace;">int </span>number, which looks a bit weird. <br />
<br />
Differently from C#, <span style="font-family: "Courier New",Courier,monospace;">const</span>(s) are necessarily <span style="font-family: "Courier New",Courier,monospace;">static</span>, while overloading operators are not static. It doesn't make a big difference, but now that I think of it, it makes more sense, because constants are defined regardless of the object instance, while operators do work on instances. <br />
<br />
Two nice Dart features that helped to shorten the code, are constructor initialization (now also featured in C# 6.0) and property getter/setter with the <span style="font-family: "Courier New",Courier,monospace;">=></span> syntax. For example:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">int get ElapsedMilliseconds => getTime() - start_time; </span><br />
<br />
This syntax is not very intuitive and one needs to get used to it; but reading the Dart documentation it seems to be coherent with the approach of having functions as first-class citizens (something I really miss in C#).<br />
<br />
The code shortens also for <span style="font-family: "Courier New",Courier,monospace;">abstract </span>classes, once a class is declared abstract there is no need to specify "<span style="font-family: "Courier New",Courier,monospace;">abstract</span>" for methods, nor to write the "<span style="font-family: "Courier New",Courier,monospace;">override</span>" keyword when implementing the member. <br />
<br />
Another nice feature is string interpolation, which is the equivalent of C#'s <span style="font-family: "Courier New",Courier,monospace;">String.Format</span>. DartEditor is able to perform in-string syntax highlight for string interpolation. Unexpected, very good.<br />
<br />
Parameters by reference: apparently Dart doesn't have an equivalent of the "<span style="font-family: "Courier New",Courier,monospace;">ref</span>" C# keyword (I guess it's the same for "<span style="font-family: "Courier New",Courier,monospace;">out</span>"). But I discovered in my code the only "<span style="font-family: "Courier New",Courier,monospace;">ref</span>" usage was unnecessary, so I just dropped it. I like Dart's design here, arguments by reference make code unreadable. If more than a return parameter is needed, why not returning a structured object? (easy in true dynamic languages).<br />
<br />
The code conversion became more complex when I had to do JavaScript interop. In this regard the C# Saltarelle compiler is more practical because gives you full access to JavaScript by the mean of the<span style="font-family: "Courier New",Courier,monospace;">[InlineCode</span>] attribute. Here C# is totally different from Dart: the C# compiler doesn't pretend to live in a proprietary VM, but assumes from the start the code is running within the JavaScript VM, so all objects are accessible and tricked to be true C# objects (by the mean of <span style="font-family: "Courier New",Courier,monospace;">[InlineCode]</span> and other attributes like <span style="font-family: "Courier New",Courier,monospace;">[Imported]</span>). Working with inline JavaScript code is similar to going down at the assembler level, it can be more efficient but it's no longer C#. But that fits perfectly in the strange world of C# run by the browser.<br />
<br />
Dart can't do the same because it's meant to run in its separate VM, and has to work the regardless whether it's running in a browser or not. So to be able to access JavaScript objects in Dart, you have to do interop.<br />
<br />
Interop is really tedious to write, to a certain extent it resembles working with <span style="font-family: "Courier New",Courier,monospace;">Reflection </span>in C#. But luckily for me I had just one object to convert. It's called <span style="font-family: "Courier New",Courier,monospace;">MersenneTwister</span>, and it's used for the sole reason of providing the same random numbers across the two different implementations. Since the raytraced scene is generated randomly, I want exactly the same scene in both C# and Dart so I can compare their execution speeds. In a different context I would have used Dart's native <span style="font-family: "Courier New",Courier,monospace;">Random </span>functions.<br />
<br />
That's why I have a custom <span style="font-family: "Courier New",Courier,monospace;">Random </span>(and DartEditor will warn of the name conflict) and also a custom <span style="font-family: "Courier New",Courier,monospace;">StopWatch</span>, just to be sure time measurements are the same between the two implementations. <br />
<br />
I was very happy to see that the interfaces for working with the canvas (<span style="font-family: "Courier New",Courier,monospace;">CanvasElement</span>, <span style="font-family: "Courier New",Courier,monospace;">CanvasRenderingContext2D</span> and <span style="font-family: "Courier New",Courier,monospace;">ImageData</span>) are almost identical to C#, so I had to do only minor changes.<br />
<br />
Now it's finished, the code is completely converted.<br />
<br />
I click run, Dartium quickly opens and the familiar raytraced image is rendered on the screen. Cool, I didn't expect it to work at the first attempt!!!<br />
<br />
Being to good to be true, I check for caching, fearing that the old C# was called in some way. But no, it's really Dart code running and doing ray-tracing! Hurray!<br />
<br />
After that, it took me a while and lot of googling to discover how to generate the needed JavaScript files. While developing, your program will run in Dartium within the DartVM. You can "<span style="font-family: "Courier New",Courier,monospace;">Manage Launches</span>" and run on generic browsers too, but if you want your JavaScript files generated for deploy, you have to click the tool option "<span style="font-family: "Courier New",Courier,monospace;">Pub Build</span>".<br />
<br />
Ok. Done. All is ready. Let's see the benchmark results!<br />
<h2>
Results</h2>
I tested the program my old AMD Athlon II X4 640 CPU, running @3GHZ with 8GBRAM and Windows 7 X64. It's the same machine I used two years ago for the JSIL-vs-Saltarelle test.<br />
<br />
Browsers:<br />
<ul>
<li>Firefox: 31.0</li>
<li>Chrome: 36.0.19</li>
<li>Chromium: 36.0.19 </li>
<li>Internet Explorer: 11.0.96</li>
</ul>
The program is set to draw the random scene pictured above, in a 640x480 canvas and report the average time per pixel spent in calculation (expressed in milliseconds). <br />
<br />
Since there is a certain variability among different runs, I repeated the test several times taking the smallest number (this aint very scientific, I know). Such variability is evident in Chrome, where the first run is usually a lot faster. Refreshing the page never gives the same fast numbers. Maybe it's because of garbage collector kicking in, who knows! <br />
<h2>
Dart Results: </h2>
When run in Chromium (aka Dartium) Dart can be executed in native mode because of the embedded DartVM. In all other cases, Dart code needs to be transpiled to JavaScript. <br />
<ol>
<li><span style="font-family: "Courier New",Courier,monospace;">Dartium (DartVM) unchecked mode: <b>0.002,8</b> ms/pixel</span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Dartium (DartVM) checked mode: 0.006,3 ms/pixel </span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Chrome (JavaScript): <b>0.025,6</b> ms/pixel</span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Explorer (JavaScript): 0.108,3 ms/pixel (*)</span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Firefox (JavaScript): 0.112,9 ms/pixel</span></li>
</ol>
(*) For some strange reason, Explorer didn't work from outside of the DartEditor, giving the exception "<span style="font-family: "Courier New",Courier,monospace;">unable to bind to a null object</span>". <br />
<h2>
C# Results:</h2>
These numbers come from running the C# program (Chrome result is different from the two years ago test, due to browser updates). <br />
<ul>
<li><span style="font-family: "Courier New",Courier,monospace;">Firefox: <b>0.000,3</b> ms/pixel</span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Chrome: 0.003,6 ms/pixel</span></li>
<span style="font-family: "Courier New",Courier,monospace;">
</span>
<li><span style="font-family: "Courier New",Courier,monospace;">Explorer: 0.012,6 ms/pixel</span></li>
</ul>
<h2>
Remarks</h2>
First we notice the native Dart VM fulfils the promise of being faster than compiled JavaScript. It's a 10x increase between unchecked mode and the fastest JavaScript browser. Not bad, but we expected that.<br />
<br />
What we didn't expect is the poor performance of DartVM compared to C#. They are on the same order of magnitude (Dartium 0.002 ms vs Chrome 0.003 ms), meaning that there is no big difference of using transpiled JavaScript or going native with Dart. If that holds true, the whole Dart purpose is in danger!<br />
<br />
What are the reason of this? Hard to say. For sure Javascript runtimes have benefited from browsers war between vendors, while Dart is young and has no competitors. This is confirmed if you look at the numbers of the two years ago test: you will notice that, on the same machine, Chrome has halved its execution speed, in just two years!<br />
<br />
Anyway what is totally disappointing for me is the even worse performance of the compiled JavaScript (<span style="font-family: "Courier New",Courier,monospace;">dart2js</span>). It's ten time slower than the C# counterpart! It is evidently slower even without doing any measurement, just by eye. Not only, Dart output is also bigger in terms of file size. The file <span style="font-family: "Courier New",Courier,monospace;">dart_raytracerdemo.dart.js</span> is 360 KB long (unminified), while the C# counterpart (<span style="font-family: "Courier New",Courier,monospace;">mscorlib.js</span>+<span style="font-family: "Courier New",Courier,monospace;">raytracerdemo.js</span>) is only 139 KB. The Dart team has a lot to improve here.<br />
<h2>
Conclusion</h2>
Overall, it was an interesting experience to port this program from C# to Dart. My conclusion is that Dart is a promising language, featuring all that I miss in C#, but still too young to go all-in with it. But it's also true that Microsoft failed to adopt C# as main web development language, so I would be happy if Dart takes its place one day.<br />
<h2>
Edit of 23 Aug: Tests repeated</h2>
As I've been asked to report data differently and do more accurate measurements, I've repeated the whole benchmarks with some changes:<br />
<ul>
<li>I've measured total elapsed time in place of time per pixel; it doesn't change much but it easier to understand.</li>
<li>The scene was made more complex by having 300 random spheres instead of 30. This increases the overall elapsed time roughly by 10x, making the measurements less variable.</li>
<li>I've also compiled a "native C#" version of it, in order to compare Dart VM vs Microsoft VM (aka CLR or MSIL). </li>
</ul>
<h2>
Edit of 24 Aug: added TypeScript</h2>
<div>
<a href="https://plus.google.com/u/0/+FreemenMuaddib/about">+Freemen Muaddib</a> suggested to add a native JavaScript implementation to the benchmark, so this morning I derived one by porting the source first to <a href="http://www.typescriptlang.org/">TypeScript</a> and then compiling to JavaScript.<br />
<br />
It was an harder work than the Dart port, because in TypeScript type annotations have a different (and more clumsy) syntax. Also, no operator overloads (it was an hell to work with vector math) and you have always to reference to "<span style="font-family: Courier New, Courier, monospace;">this</span>" for class members, even when you are within the class itself. In other words, TypeScript inherits all the JavaScript quirks.</div>
<div>
<br /></div>
<div>
Anyway Typescript resulted the fastest among all in Chrome. See the updated table of results.<br />
<h2>
Edit of 25 Aug: improved Dart code</h2>
<a href="https://plus.google.com/+StephanHerhut">+StephanHerhut</a> suggested to initialize two fields to 0.0, because explicit initialization, especially for static fields, helps type inference in Dart. This is also semantically consistent with the C# version, because in a certain sense, it simulates non-nullability of the fields. </div>
<div>
</div>
<div>
This simple fix resulted in a ~50% speed increase for <span style="font-family: "Courier New",Courier,monospace;">dart2js </span>compiled code and ~10% for DartVM. The native execution was also improved with <a href="https://plus.google.com/+VyacheslavEgorov">+VyacheslavEgorov</a>'s suggestion of using double constants instead of integer constants when doing compare operations (eg. <span style="font-family: "Courier New",Courier,monospace;">if(r>255.0)</span> in place of <span style="font-family: "Courier New",Courier,monospace;">if(r>255)</span>). <br /><br />See the updated results.<br /></div>
<div style="text-align: center;">
the new 3d test scene</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAU9q7Zpmp5TBnFxMGAe1C40xCSiJ3lFol3F1y2xJX112XzOuaR944c66e2l_D6Sz0cLiib5IxuWd5QoLBh6bsAXX5Aq3XqBMOqiTbbv6dc_-8uIajuQeuO-1v43BRD1vJwGvjTDWpdKc/s1600/canvas1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAU9q7Zpmp5TBnFxMGAe1C40xCSiJ3lFol3F1y2xJX112XzOuaR944c66e2l_D6Sz0cLiib5IxuWd5QoLBh6bsAXX5Aq3XqBMOqiTbbv6dc_-8uIajuQeuO-1v43BRD1vJwGvjTDWpdKc/s1600/canvas1.png" height="240" width="320" /></a></div>
<br />
<div style="text-align: center;">
<br /></div>
<div style="text-align: center;">
<b>new results, time is expressed in seconds</b></div>
<div style="text-align: center;">
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: center;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7mVqCT0OUUACrRWaAcUAlqEVtoE3SyljFMcGNRzdYzzpiwwIq74LogrpExQtX8KN7MqvxhiQu3ykgm9bhu9kdJdQYFfpdmIg9GxdOJm9HSmR_FbeGe5k9j82fZ2S3VWIKUUoMcwOfnoA/s1600/table.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7mVqCT0OUUACrRWaAcUAlqEVtoE3SyljFMcGNRzdYzzpiwwIq74LogrpExQtX8KN7MqvxhiQu3ykgm9bhu9kdJdQYFfpdmIg9GxdOJm9HSmR_FbeGe5k9j82fZ2S3VWIKUUoMcwOfnoA/s1600/table.png" height="252" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8799XI5FKPmFp20Lcgk-V0mdKwvydG5GdsTY7afuTsEeYolahoE5S618F-Gf3otQXFuniw3adw1NzFFLOLqXonEHN7wYLu__KHBttz9nrce1PSeJYQuYkf84gdHoTYvt5Ue-h50uJcb4/s1600/graph.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8799XI5FKPmFp20Lcgk-V0mdKwvydG5GdsTY7afuTsEeYolahoE5S618F-Gf3otQXFuniw3adw1NzFFLOLqXonEHN7wYLu__KHBttz9nrce1PSeJYQuYkf84gdHoTYvt5Ue-h50uJcb4/s1600/graph.png" height="291" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h2>
Final remarks</h2>
<ul>
<li>TypeScript is the fastest among all. </li>
<li>Dart VM is the faster than Microsoft VM, it's a surprise.</li>
<li>Transpiled C# does a nice job competing both with Dart VM and Microsoft VM. </li>
<li>Dart2Js has a lot to improve</li>
</ul>
<br />
The <a href="https://github.com/nippur72/Dart.RayTracerDemo">Github repo</a> now contains all the versions used for this test: Dart, C# Saltarelle, C# native and TypeScript.<br />
<br />
<br />
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>Antonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.com0tag:blogger.com,1999:blog-681966504815228096.post-20673079249869805212012-12-28T03:01:00.001-08:002012-12-28T03:01:20.240-08:00A MetaStock class library in C#<a href="http://en.wikipedia.org/wiki/MetaStock">Metastock</a> is a popular data format for storing end-of-day and intraday stock quotes data.<br />
<br />
It's a very old format with a structure that is a total mess. I myself have spent a lot of time figuring how to properly read quotes in a jungle of different files and formats.<br />
<br />
Here I present the actual C# code I use to interface with the database that I update every evening from my data vendor.<br />
<br />
Information on the metastock data structure is scattered over the web. I've collected what seemed to work and implemented as a C# class. A special thank to <a href="http://www.boyet.com/Articles/MBFSinglePrecision.html">Julian M Bucknall</a> for providing the code to convert from MBF4 (Microsoft Binary Format single precision) to standard floating point IEEE number.<br />
<br />
The library is organized as follows:<br />
<ul>
<li>the struct <span style="font-family: "Courier New",Courier,monospace;">MetaStockRow </span>stores one day of data </li>
</ul>
<blockquote>
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> public struct MetaStockRow
{
public DateTime Day;
public double Open;
public double High;
public double Low;
public double Close;
public double Volume;
public double OpenInterest;
}
</code></pre>
</blockquote>
<ul>
<li>a set of <span style="font-family: "Courier New",Courier,monospace;">MetaStockRow</span>(s) forms a <span style="font-family: "Courier New",Courier,monospace;">MetaStockTable</span>, that is, the whole data for a stock symbol</li>
<li>a set of <span style="font-family: "Courier New",Courier,monospace;">MetaStockTable</span>(s) forms a <span style="font-family: "Courier New",Courier,monospace;">MetaStockCollection</span>, which is useful when you have to work with more than one stock at a time. A special feature of <span style="font-family: "Courier New",Courier,monospace;">MetaStockCollection </span>is the capability to intersect dates, that is, making sure that the stocks you are working with have the same data points (days).</li>
<li><span style="font-family: "Courier New",Courier,monospace;">MetaStockTables </span>and <span style="font-family: "Courier New",Courier,monospace;">MetaStockCollections </span>are generated by the class <span style="font-family: "Courier New",Courier,monospace;">MetaStockReader </span>which is the main object that performs the low-level reading on the database files. </li>
</ul>
<ul>
</ul>
How to use the code:<br />
<ul>
<li>Create a <span style="font-family: "Courier New",Courier,monospace;">MetaStockReader </span>object</li>
<li>Use <span style="font-family: "Courier New",Courier,monospace;">AddPath()</span> to tell it where your metastock folders are located (usually there are more than one). </li>
<li>Create a <span style="font-family: "Courier New",Courier,monospace;">MetaStockCollection </span>object</li>
<li> Insert stocks in the collection with <span style="font-family: "Courier New",Courier,monospace;">AddSymbol()</span></li>
<li>Trim and synch dates in the collection with <span style="font-family: "Courier New",Courier,monospace;">FilterPeriod()</span> and <span style="font-family: "Courier New",Courier,monospace;">IntersectDates()</span></li>
<li>Get daily closes with <span style="font-family: "Courier New",Courier,monospace;">GetCloseArray()</span></li>
</ul>
<br />
An example:<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> MetaStockReader MR = new MetaStockReader();
MR.AddPath(@"C:\MetaStock\NYSE");
MR.AddPath(@"C:\MetaStock\AMEX");
MetaStockCollection ML = new MetaStockCollection();
ML.AddSymbol("MSFT",MR);
ML.AddSymbol("BRK.A",MR);
ML = ML.FilterPeriod(DateTime.Parse("01/01/2000"),DateTime.Parse("31/12/2011"));
ML = ML.IntersectDates();
double[] s1 = ML[0].GetCloseArray();
double[] s2 = ML[1].GetCloseArray();
</code></pre>
<br />
If you need to work with just one stock, you can avoid using <span style="font-family: "Courier New",Courier,monospace;">MetaStockCollection </span>and work directly with <span style="font-family: "Courier New",Courier,monospace;">MetaStockTable </span>objects and access to its <span style="font-family: "Courier New",Courier,monospace;">MetaStockRow</span>(s).<br />
<br />
There is a little more in the code of what I've shown here, but if you have a look at the sources you can easily discover by yourself all the other features. I must warn you the code quality is rather poor, because I haven't spent much time in improving it. I stopped when it "worked" (shame on me!). <br />
<br />
Download the C# source code <a href="http://antoninoporcino.xoom.it/quantbucket/MetaStock.zip">here</a>. <br />
<br />
<br />
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>Antonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.com3tag:blogger.com,1999:blog-681966504815228096.post-88843780183726673852012-12-01T05:53:00.001-08:002013-03-07T04:15:50.575-08:00Saltarelle vs JSIL -- Raytracer DemoI've compared the performance of <a href="http://www.saltarelle-compiler.com/">Saltarelle-compiler </a>vs. another C# to Javascript compiler: <a href="http://jsil.org/">JSIL </a>by <a href="http://www.luminance.org/">Kevin Gadd</a>.<br />
<br />
Differently from Saltarelle, JSIL operates at intermediate language (IL) level. After the C# program is compiled by Visual Studio, JSIL examines the MSIL instruction codes contained in the dll files and translates them into javascript.<br />
<br />
This allows a greater compatibility and portability of .NET applications, <strike>but probably introduces an unnecessary layer of abstraction between the original C# code and the javascript output</strike> but the additional code needed to simulate all the .NET library has a negative impact on the execution of the compiled code.<br />
<br />
This is shown by the following test.<br />
<br />
I've converted the <a href="http://hildr.luminance.org/Raytracer/Raytracer.html">Raytracer Demo</a> featured on the JSIL website to the Saltarelle compiler, with the aim to compare the execution speed of the two versions.<br />
<br />
Adapting the code to Saltarelle was easy but not straightforward. Saltarelle was missing some needed <span style="font-family: Courier New, Courier, monospace;">System </span>classes that I had to re-implement, namely <span style="font-family: Courier New, Courier, monospace;">Bitmap</span>, <span style="font-family: Courier New, Courier, monospace;">Color</span>, <span style="font-family: Courier New, Courier, monospace;">StopWatch</span>, <span style="font-family: Courier New, Courier, monospace;">Console </span>and <span style="font-family: Courier New, Courier, monospace;">Random</span>. I tried to keep them as similar as possible to the JSIL version so to not affect the result of the test. I also had to write the import code for <span style="font-family: Courier New, Courier, monospace;">mersenne.js</span>, a small library the demo uses for generating random numbers (probably because javascript's <span style="font-family: Courier New, Courier, monospace;">Math.random()</span> lacks the random seed feature).<br />
<br />
<h3>
Result of the test</h3>
I ran the test on a four core Athlon AMD processor with Windows 7 and Chrome browser. The result is summarized in the table:<br />
<br />
<table style="border: 1px solid gray; cell-padding: 5px; text-align: center;">
<tbody>
<tr><td></td><td><span style="font-family: Courier New, Courier, monospace;"><b>Saltarelle</b></span></td><td><span style="font-family: Courier New, Courier, monospace;"><b>JSIL</b></span></td></tr>
<tr><td><span style="font-family: Courier New, Courier, monospace;">Script code size (.js)</span></td><td><span style="font-family: Courier New, Courier, monospace;">108 KB</span></td><td><span style="font-family: Courier New, Courier, monospace;">15 MB</span></td></tr>
<tr><td><span style="font-family: Courier New, Courier, monospace;">Average time/pixel</span></td><td><span style="font-family: Courier New, Courier, monospace;">0.006 ms</span></td><td><span style="font-family: Courier New, Courier, monospace;">0.093 ms</span></td></tr>
</tbody></table>
<br />
So, on average, Saltarelle is 15 times faster than JSIL. Impressive!<br />
<br />
The output script code is also very small-sized with no loading time, while the JSIL version has to download 15 MB of stuff before it can start.<br />
<br />
<h3>
Check by yourself</h3>
<div>
Run the above test directly in your browser:</div>
<div>
<ul>
<li><a href="http://hildr.luminance.org/Raytracer/Raytracer.html">JSIL version</a> by Kevin Gadd</li>
<li><a href="http://antoninoporcino.xoom.it/Saltarelle/RayTracerDemo/Saltarelle%20Raytracer%20Demo.htm">Saltarelle version</a> by Nino Porcino</li>
</ul>
</div>
You can also download the <a href="https://github.com/nippur72/Saltarelle.RaytracerDemo">sources hosted on GitHub</a> .<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>Antonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.com3tag:blogger.com,1999:blog-681966504815228096.post-20759325483506628132011-06-20T09:54:00.000-07:002011-06-20T10:09:31.753-07:00Indirizzo del sitoIl mio vecchio sito (quello statico HTML) ha cambiato indirizzo, adesso si trova su <a href="http://antoninoporcino.xoom.it/">http://antoninoporcino.xoom.it</a>.<br /><br />Ciò è dovuto al fatto che il provider (Virgilio/Xoom) ha fatto dei cambiamenti e il mio sito è sparito all'improvviso. Adesso l'ho ricreato, ma tutti i link che puntavano ad esso non sono più validi.<br /><br />Inoltre sono sparito pure dai motori di ricerca! (spero temporaneamente).Antonino Porcinohttp://www.blogger.com/profile/11749887898485061928noreply@blogger.com0