<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Programmer with Microsoft tools &#187; C#</title>
	<atom:link href="http://msprogrammer.serviciipeweb.ro/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://msprogrammer.serviciipeweb.ro</link>
	<description>A programmer journey through code, books and tools</description>
	<lastBuildDate>Mon, 20 Feb 2012 05:57:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>runtime loading versus compile time loading</title>
		<link>http://msprogrammer.serviciipeweb.ro/2011/11/14/runtime-loading-versus-compile-time-loading/</link>
		<comments>http://msprogrammer.serviciipeweb.ro/2011/11/14/runtime-loading-versus-compile-time-loading/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 04:07:29 +0000</pubDate>
		<dc:creator>Andrei Ignat</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://msprogrammer.serviciipeweb.ro/?p=908</guid>
		<description><![CDATA[How is started :
We have had a .NET meeting in Romania where one of the presenters load the MaxMind GEO-IP database to see from where the user comes from.
He loads from csv into an List .
He was insisting on the speed of the algorithm to find IP range.
And I was thinking : ok, the algorithm [...]]]></description>
			<content:encoded><![CDATA[<p>How is started :</p>
<p>We have had a .NET meeting in Romania where one of the presenters load the MaxMind GEO-IP database to see from where the user comes from.<br />
He loads from csv into an List<GeoLocation> .<br />
He was insisting on the speed of the algorithm to find IP range.</p>
<p>And I was thinking : ok, the algorithm   speed is one argument.</p>
<p>What if, instead of loading at runtime the csv file and putting into an List<GeoLocation> , I will load at compile time ?</p>
<p>So I have made a .tt file that parses the MaxMind csv file and generates something like</p>
<pre class="brush: csharp;">
public class Location:List&lt;GeoLocation&gt;
    {
        public Location():base()
        {
                            this.Add(new GeoLocation(1, &quot;O1&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 0f, 0f));
                            this.Add(new GeoLocation(2, &quot;AP&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 35f, 105f));
                            this.Add(new GeoLocation(3, &quot;EU&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 47f, 8f));
                            this.Add(new GeoLocation(4, &quot;AD&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 42.5f, 1.5f));
                            this.Add(new GeoLocation(5, &quot;AE&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 24f, 54f));
                            this.Add(new GeoLocation(6, &quot;AF&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 33f, 65f));
                            this.Add(new GeoLocation(7, &quot;AG&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 17.05f, -61.8f));
                            this.Add(new GeoLocation(8, &quot;AI&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 18.25f, -63.1667f));
                            this.Add(new GeoLocation(9, &quot;AL&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 41f, 20f));
                            this.Add(new GeoLocation(10, &quot;AM&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 40f, 45f));
                            this.Add(new GeoLocation(11, &quot;AN&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 12.25f, -68.75f));
                            this.Add(new GeoLocation(12, &quot;AO&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, -12.5f, 18.5f));
                            this.Add(new GeoLocation(13, &quot;AQ&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, -90f, 0f));
//and so on
 </pre>
<p>       The .cs generated file has 30 MB. The compiled exe of a test Console application have some 19 MB.</p>
<p>I have put  each code into a console application and the results were:</p>
<p>The Console application that have  csv parser loads in 1 second  This is the runtime loading the file.<br />
The  Console application that have the class with all predefined  does not loads after 1 minute  &#8211; and the memory is increasing. This is the compiletime loading the contents.</p>
<p>And this is the problem  &#8211; after all, the csv parser loads all those in memory the same &#8211; and , more, it&#8217;s the hard drive access time that counts. So the compiled one should be faster , right ?<br />
Not so fast . The JIT comes into action . And it compiles the exe. So it takes MORE time.<br />
I submit the question to some list and they come with 2(big) suggestions:</p>
<p>Suggestion 1 : NGEN-ing  takes 1 hour on my PC (x64,  4 GB of RAM, 4 core ) and did not finish.( 4GB used at maximum). Not a good idea apparently.<br />
Suggestion 2 : put struct. Same time&#8230;</p>
<p>For you to try please download </p>
<p>http://msprogrammer.serviciipeweb.ro/wp-content/uploads/runcomp.7z</p>
<p>However, the  final question arises : from what number of data we should load from runtime insteand of compiletime ?<br />
( For 1 item,the compile time is better. For 2, the same. &#8230; For all data in the csv, &#8211; runtime is required)<br />
What I  expect is a function that takes a parameter ( data that have x bytes long) and says :</p>
<p>loading < 1000 records is faster on compile time rather than from hard disk ( runtime)<br />
loading > 2000 records is faster on hard disk ( runtime) rather from compile time</p>
<p>From 1000 to 2000 depends on RAM, RPM and others<br />
( or some algorythm for that)</p>
<p>How we calculate this number ?<br />
( Take note is a pure mathematical question of minimizing the time by using both compile time and run time . Does not matter in practice since the time in runtime is so small)</p>
]]></content:encoded>
			<wfw:commentRss>http://msprogrammer.serviciipeweb.ro/2011/11/14/runtime-loading-versus-compile-time-loading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

