<?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>Order in Chaos &#187; Code</title>
	<atom:link href="http://orderinchaos.org/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://orderinchaos.org</link>
	<description>the personal weblog of dave kennedy</description>
	<lastBuildDate>Mon, 15 Aug 2011 23:31:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>#OptimizeCode For L2 Cache</title>
		<link>http://orderinchaos.org/2009/11/optimizecode-for-l2-cache/</link>
		<comments>http://orderinchaos.org/2009/11/optimizecode-for-l2-cache/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 08:15:45 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://orderinchaos.org/?p=308</guid>
		<description><![CDATA[I&#8217;ve had a couple discussions lately discussing the difference in speed between accessing data from memory and disk. In most of these conversations people were surprised to know that there is something faster than Main Memory that the programmer can take advantage of .
First, let&#8217;s consider the memory hierarchy. If you do a google image [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a couple discussions lately discussing the difference in speed between accessing data from memory and disk. In most of these conversations people were surprised to know that there is something faster than Main Memory that the programmer can take advantage of .</p>
<p>First, let&#8217;s consider the memory hierarchy. If you do a google image search for memory hierarchy, you&#8217;ll see a few different (and very insightful) interpretations. In it&#8217;s simplest, it looks like this.</p>
<div id="attachment_319" class="wp-caption alignright" style="width: 410px"><a href="http://orderinchaos.org/wp-content/uploads/2009/11/memoryhierarchysm.jpg"><img class="size-full wp-image-319" title="memory hierarchy" src="http://orderinchaos.org/wp-content/uploads/2009/11/memoryhierarchysm.jpg" alt="A version of the memory hierachy. L0 = Level 0, L1 = Level 2, and so on. It should be noted that some high-end systems have a third SRAM Cache, which becomes L3. This causes L3 (Main Memory) to become L4 and L4 to become L5." width="400" height="339" /></a><p class="wp-caption-text">A version of the memory hierarchy. L0 = Level 0, L1 = Level 2, and so on. It should be noted that some high-end systems have a third SRAM Cache, which becomes L3. This causes L3 (Main Memory) to become L4 and L4 to become L5. Also, please also keep in mine that the higher you move up the memory hierarchy, the more monetarily expensive things become (5 GB of RAM is more expensive than 5 GB of hard disk space).</p></div>
<p>Main memory is DRAM, while Level 2 (L2) Cache is SRAM. Now, I hear you asking, &#8220;What about my SDRAM? Is that the best of both worlds?&#8221; No, it&#8217;s still DRAM. The S means something different.</p>
<ul>
<li>DRAM: Dynamic Random Access Memory</li>
<li>SRAM: Static Random Access Memory</li>
<li>SDRAM: Synchronous Dynamic Random Access Memory</li>
<li>DDR SDRAM: Double Data Rate &amp; one hell of an acronym.</li>
</ul>
<p>If you want to access something on your hard disk, it takes roughly 100,000 times longer than DRAM, and 1,000,000 times longer than SRAM, Making SRAM ~ 10x faster than DRAM.</p>
<p>There are actually two, and in some machines even three, levels of SRAM Cache. L1 actually lives on-board the processor, while L2 (and the less common L3) live off-board. Although they&#8217;re typically all SRAM, in this post references to &#8220;SRAM&#8221; specifically mean L2 Cache.</p>
<p>Now I hear you say &#8220;Awesome! I&#8217;m building this killer machine, and it&#8217;s got all the Main Memory it can handle, but I&#8217;ve still got some money to burn. Can I increase it&#8217;s SRAM too?&#8221; No. SRAM (L2 Cache) lives in a piece of additional hardware on your motherboard, you can&#8217;t really do anything to it.</p>
<p>So what&#8217;s it all about then? In general, the majority of your accesses to memory will be very close, even adjacent, to your previous access. It won&#8217;t always be the case, but it probably will. Caches take advantage of this probability. If you access memory location [x], there is a high probability that you&#8217;re next memory access will be to memory location [x+1] or something similar. If you accessed an int in memory, which has a typical size of 4 bytes, then your next memory access will probably  be [x+4]. If L2 Cache is nearly 10x faster, as we know what you&#8217;re <em>probably</em> access next, we can speed things up by automatically moving what you&#8217;ll probably move into L2 Cache. When your program makes it&#8217;s next memory request, we check to see if it&#8217;s already in L2. If it is (cache hit) great! If it&#8217;s not, (cache miss), too bad &#8211; we goto main memory and swap that and it&#8217;s adjacent bytes, into L2 and proceed. A typical Intel Pentium chip has 128 KB &#8211; 2 MB of L2 Cache, and typically swaps data in and out of cache from main memory in 32 byte chunks.</p>
<p>So how can a programmer take advantage of this? If you&#8217;re going to be accessing chunks of memory, try to access it as you know it appears spatially in memory.</p>
<p>Let us consider the loop below, and assume for sake of example,</p>
<ul>
<li>Length = 8</li>
<li>Any given array[i], an integer, is 4 bytes</li>
<li>L2 Cache swaps data in 16 byte chunks (as opposed to the actual 32, noted above. That&#8217;s 4 integers).</li>
<li>At the start of the loop, the L2 Cache was empty (cold cache).</li>
</ul>
<p>[cc lang="cpp"]<br />
int sumArray(int array[Length])<br />
{<br />
   int sum = 0;<br />
   for (int i = 0; i &lt; Length; i++)<br />
      sum += array[i];<br />
   return sum;<br />
}<br />
[/cc]<br />
The below table represents cache hits and misses. White are cache hits, and red are cache misses.</p>
<table style="border-width:1; border-style:solid; border-collapse:  collapse; " border="0">
<tbody>
<tr>
<td><strong>array[i]</strong></td>
<td><strong><span style="color: #ff0000;">i=1</span></strong></td>
<td><strong><span style="color: #ffffff;">i=2</span></strong></td>
<td><strong><span style="color: #ffffff;">i=3</span></strong></td>
<td><strong><span style="color: #ffffff;">i=4</span></strong></td>
<td><strong><span style="color: #ff0000;">i=5</span></strong></td>
<td><strong><span style="color: #ffffff;">i=6</span></strong></td>
<td><strong><span style="color: #ffffff;">i=7</span></strong></td>
<td><strong><span style="color: #ffffff;">i=8</span></strong></td>
</tr>
<tr>
<td><strong>Access Order</strong></td>
<td><strong><span style="color: #ff0000;">1</span></strong></td>
<td><strong><span style="color: #ffffff;">2</span></strong></td>
<td><strong><span style="color: #ffffff;">3</span></strong></td>
<td><strong><span style="color: #ffffff;">4</span></strong></td>
<td><strong><span style="color: #ff0000;">5</span></strong></td>
<td><strong><span style="color: #ffffff;">6</span></strong></td>
<td><strong><span style="color: #ffffff;">7</span></strong></td>
<td><strong><span style="color: #ffffff;">8</span></strong></td>
</tr>
</tbody>
</table>
<p>This is the best that we can do. Now let us consider a 2 dimensional array, given the assumtions</p>
<ul>
<li>Rows = 8</li>
<li>Columns = 4</li>
<li>C stores arrays/matrices in row-major order.</li>
<li>The rest is the same as the example above.</li>
</ul>
<p>[cc lang="c++"]<br />
int sumMatrix(int matrix[Columns][Rows])<br />
{<br />
   int sum = 0;<br />
   for (int col = 0; col &lt; Columns; col++)<br />
      for (int row = 0; row &lt; Rows; row++)<br />
         sum += matrix[col][row];<br />
   return sum;<br />
}<br />
[/cc]</p>
<p>Let&#8217;s look at those cache hits and misses. The values in the table body are the order in which matrix[col][row] was accessed.</p>
<table style="border-width:1; border-style:solid; border-collapse:  collapse; " border="0">
<tbody>
<tr>
<td><strong> matrix[col][row]</strong></td>
<td><strong><span style="color: #ff0000;">row=0 </span><br />
</strong></td>
<td><strong><span style="color: #ffffff;">row=1</span><br />
</strong></td>
<td><strong><span style="color: #ffffff;">row=2 </span><br />
</strong></td>
<td><span style="color: #ffffff;"><strong>row=3</strong> </span></td>
<td><span style="color: #ff0000;"><strong>row=4</strong> </span></td>
<td><strong><span style="color: #ffffff;">row=5</span></strong></td>
<td><span style="color: #ffffff;"><strong>row=6</strong> </span></td>
<td><strong><span style="color: #ffffff;">row=7 </span></strong></td>
</tr>
<tr>
<td><strong>col = 0</strong></td>
<td><strong><span style="color: #ff0000;"> 1</span></strong></td>
<td><strong><span style="color: #ff0000;"><span style="color: #ffffff;">2 </span><br />
</span></strong></td>
<td><span style="color: #ffffff;"><strong>3</strong> </span></td>
<td><strong><span style="color: #ffffff;">4</span></strong></td>
<td><strong><span style="color: #ff0000;"> 5</span></strong></td>
<td><strong><span style="color: #ffffff;">6</span></strong></td>
<td><strong><span style="color: #ffffff;">7</span></strong></td>
<td><strong><span style="color: #ffffff;"> 8</span></strong></td>
</tr>
<tr>
<td><strong> col = 1</strong></td>
<td><strong> <span style="color: #ff0000;">9</span></strong></td>
<td><strong> <span style="color: #ffffff;">10</span></strong></td>
<td><strong><span style="color: #ffffff;"> 11</span></strong></td>
<td><strong><span style="color: #ffffff;"> 12</span></strong></td>
<td><strong><span style="color: #ff0000;">13</span></strong></td>
<td><strong> <span style="color: #ffffff;">14</span></strong></td>
<td><strong><span style="color: #ffffff;">15</span></strong></td>
<td><strong><span style="color: #ffffff;"> 16</span></strong></td>
</tr>
<tr>
<td><strong>col = 2</strong></td>
<td><strong><span style="color: #ff0000;"> 17</span></strong></td>
<td><strong> <span style="color: #ffffff;">18</span></strong></td>
<td><strong><span style="color: #ffffff;"> 19</span></strong></td>
<td><strong><span style="color: #ffffff;"> 20</span></strong></td>
<td><strong><span style="color: #ff0000;">21</span></strong></td>
<td><strong><span style="color: #ffffff;">22</span></strong></td>
<td><strong><span style="color: #ffffff;"> 23</span></strong></td>
<td><strong><span style="color: #ffffff;"> 24</span></strong></td>
</tr>
<tr>
<td><strong> col = 3</strong></td>
<td><strong><span style="color: #ff0000;"> 25</span></strong></td>
<td><strong><span style="color: #ffffff;">26</span></strong></td>
<td><strong><span style="color: #ffffff;">27</span></strong></td>
<td><strong> <span style="color: #ffffff;">28</span></strong></td>
<td><strong><span style="color: #ff0000;"> 29</span></strong></td>
<td><strong><span style="color: #ffffff;">30</span></strong></td>
<td><strong> <span style="color: #ffffff;">31</span></strong></td>
<td><strong><span style="color: #ff0000;"> <span style="color: #ffffff;">32</span></span></strong></td>
</tr>
</tbody>
</table>
<p>Now let us consider the case where we do nothing except swap the order in which we access the matrix elements, and inspect the cache hit/miss table.<br />
[cc lang="c++"]<br />
int sumMatrix(int matrix[Columns][Rows])<br />
{<br />
   int sum = 0;<br />
   for (int row = 0; row &lt; Rows; row++)<br />
      for (int col = 0; col &lt; Columns; row++)<br />
         sum += matrix[col][row];<br />
   return sum;<br />
}<br />
[/cc]</p>
<table style="border-width:1; border-style:solid; border-collapse:  collapse; " border="0">
<tbody>
<tr>
<td><strong>matrix[col][row]</strong></td>
<td><strong>row=0</strong></td>
<td><strong>row=1</strong></td>
<td><strong>row=2</strong></td>
<td><strong>row=3</strong></td>
<td><strong>row=4</strong></td>
<td><strong>row=5</strong></td>
<td><strong>row=6</strong></td>
<td><strong>row=7</strong></td>
</tr>
<tr>
<td><strong>col=0</strong></td>
<td><span style="color: #ff0000;"><strong>1</strong></span></td>
<td><span style="color: #ff0000;"><strong>2</strong></span></td>
<td><span style="color: #ff0000;"><strong>3</strong></span></td>
<td><span style="color: #ff0000;"><strong>4</strong></span></td>
<td><span style="color: #ff0000;"><strong>5</strong></span></td>
<td><span style="color: #ff0000;"><strong>6</strong></span></td>
<td><span style="color: #ff0000;"><strong>7</strong></span></td>
<td><span style="color: #ff0000;"><strong>8</strong></span></td>
</tr>
<tr>
<td><strong>col=1</strong></td>
<td><span style="color: #ff0000;"><strong>9</strong></span></td>
<td><span style="color: #ff0000;"><strong>10</strong></span></td>
<td><span style="color: #ff0000;"><strong>11</strong></span></td>
<td><span style="color: #ff0000;"><strong>12</strong></span></td>
<td><span style="color: #ff0000;"><strong>13</strong></span></td>
<td><span style="color: #ff0000;"><strong>14</strong></span></td>
<td><span style="color: #ff0000;"><strong>15</strong></span></td>
<td><span style="color: #ff0000;"><strong>16</strong></span></td>
</tr>
<tr>
<td><strong>col=2</strong></td>
<td><span style="color: #ff0000;"><strong>17</strong></span></td>
<td><span style="color: #ff0000;"><strong>18</strong></span></td>
<td><span style="color: #ff0000;"><strong>19</strong></span></td>
<td><span style="color: #ff0000;"><strong>20</strong></span></td>
<td><span style="color: #ff0000;"><strong>21</strong></span></td>
<td><span style="color: #ff0000;"><strong>22</strong></span></td>
<td><span style="color: #ff0000;"><strong>23</strong></span></td>
<td><span style="color: #ff0000;"><strong>24</strong></span></td>
</tr>
<tr>
<td><strong>col=3</strong></td>
<td><span style="color: #ff0000;"><strong>25</strong></span></td>
<td><span style="color: #ff0000;"><strong>26</strong></span></td>
<td><span style="color: #ff0000;"><strong>27</strong></span></td>
<td><span style="color: #ff0000;"><strong>28</strong></span></td>
<td><span style="color: #ff0000;"><strong>29</strong></span></td>
<td><span style="color: #ff0000;"><strong>30</strong></span></td>
<td><span style="color: #ff0000;"><strong>31</strong></span></td>
<td><span style="color: #ff0000;"><strong>32</strong></span></td>
</tr>
</tbody>
</table>
<p>Every access is a cache miss! Meaning that we have to goto main memory for every access of the matrix. This will be approximately 9 times slower than if we hadn&#8217;t swapped the row/column order. This is because data is not actually stored as a two dimensional matrix in main memory &#8211; everything is a linear array, with row and column offsets being calculated. Because some data resides right next to other data in main memory, it will also appear this way when 32 bytes are moved into the cache. If you access them in the same order in which they&#8217;re stored in main memory, you can take advantage of the cache. If you don&#8217;t, you end up forcing cache two swap stuff in and out from main memory.</p>
<p><a href="http://en.wikipedia.org/wiki/John_D._Carmack">John Carmack</a> was once quoted as saying &#8220;Low level programming is good for the programmer&#8217;s soul&#8221;.</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://orderinchaos.org/2009/11/optimizecode-for-l2-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hide Email Addresses With JavaScript</title>
		<link>http://orderinchaos.org/2009/10/hide-email-addresses-with-javascript/</link>
		<comments>http://orderinchaos.org/2009/10/hide-email-addresses-with-javascript/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 05:54:47 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web crawler]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[web spider]]></category>

		<guid isPermaLink="false">http://orderinchaos.org/?p=98</guid>
		<description><![CDATA[A lot of people don&#8217;t like to put their e-mail address directly on a web-page. You may have seen e-mail addresses that look like &#8220;jondoeATgmailDOTcom&#8221;, or &#8220;j o h n do e @ gma il . c o m!&#8221;. Strangeness such as spelling out words like &#8220;AT&#8221;, odd spaces, or non e-mail address characters such [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>A lot of people don&#8217;t like to put their e-mail address directly on a web-page. You may have seen e-mail addresses that look like &#8220;jondoeATgmailDOTcom&#8221;, or &#8220;j o h n do e @ gma il . c o m!&#8221;. Strangeness such as spelling out words like &#8220;AT&#8221;, odd spaces, or non e-mail address characters such as ! are all tricks employed to avoid having your e-mail address scopped up by someone looking to spam you.</p>
<p>The way in which I approach the problem is by having javascript write my e-mail address for me. I get both a well formed and clickable e-mail link, as well as avoid getting my e-mail address scooped up by a web crawler. Now, I know that the javascript document.write() function is shunned in most circles, but below is some example code.<br />
[cc lang="javascript"]<br />
// File is contactInfo.js<br />
document.write(&#8221;<strong>David Kennedy: </strong><em><br />
Web </em> <a href="http://davidwkennedy.com"> http://davidwkennedy.com</a>|<br />
<em> E-Mail </em> <a href="mailto:dave@orderinchaos.org">dave@orderinchaos.org</a> |<br />
<em> Phone: 435.770.6865 </em>&#8220;);<br />
[/cc]<br />
Which is then called in the HTML file as seen below<br />
[cc lang="html"]<br />
<script src="contactInfo.js" type="text/javascript"></script><br />
[/cc]<br />
I&#8217;m sure that it&#8217;s probably quite possible to write a web crawler that will be smart enough to pick it up, but I think most web crawlers won&#8217;t.</p>
<p>For example, when I use the web crawler below on my personal site davidwkennedy.com you can see that the results do not contain my e-mail address!</p>
<p>Below is a web crawler, written in Python &amp; borrowed from <a href="http://www.ibm.com/developerworks/linux/library/l-spider/">IBM&#8217;s site on web spiders</a>.<br />
[cc lang="python"]<br />
#!/usr/local/bin/python</p>
<p>import httplib<br />
import sys<br />
import re<br />
from HTMLParser import HTMLParser</p>
<p>class miniHTMLParser( HTMLParser ):</p>
<p>viewedQueue = []<br />
instQueue = []</p>
<p>def get_next_link( self ):<br />
if self.instQueue == []:<br />
return &#8221;<br />
else:<br />
return self.instQueue.pop(0)</p>
<p>def gethtmlfile( self, site, page ):<br />
try:<br />
httpconn = httplib.HTTPConnection(site)<br />
httpconn.request(&#8221;GET&#8221;, page)<br />
resp = httpconn.getresponse()<br />
resppage = resp.read()<br />
except:<br />
resppage = &#8220;&#8221;</p>
<p>return resppage</p>
<p>def handle_starttag( self, tag, attrs ):<br />
if tag == &#8216;a&#8217;:<br />
newstr = str(attrs[0][1])<br />
if re.search(&#8217;http&#8217;, newstr) == None:<br />
if re.search(&#8217;mailto&#8217;, newstr) == None:<br />
if re.search(&#8217;htm&#8217;, newstr) != None:<br />
if (newstr in self.viewedQueue) == False:<br />
print &#8221;  adding&#8221;, newstr<br />
self.instQueue.append( newstr )<br />
self.viewedQueue.append( newstr )<br />
else:<br />
print &#8221;  ignoring&#8221;, newstr<br />
else:<br />
print &#8221;  ignoring&#8221;, newstr<br />
else:<br />
print &#8221;  ignoring&#8221;, newstr</p>
<p>def main():</p>
<p>if sys.argv[1] == &#8221;:<br />
print &#8220;usage is ./minispider.py site link&#8221;<br />
sys.exit(2)</p>
<p>mySpider = miniHTMLParser()</p>
<p>link = sys.argv[2]</p>
<p>while link != &#8221;:</p>
<p>print &#8220;\nChecking link &#8220;, link</p>
<p>retfile = mySpider.gethtmlfile( sys.argv[1], link )<br />
mySpider.feed(retfile)<br />
link = mySpider.get_next_link()</p>
<p>mySpider.close()</p>
<p>print &#8220;\ndone\n&#8221;</p>
<p>if __name__ == &#8220;__main__&#8221;:<br />
main()<br />
[/cc]<br />
Below are the results of the above crawler on davidwkennedy.com<br />
[cc lang="bash"]<br />
dave@dave-sparta:~$ python miniCrawler.py davidwkennedy.com /</p>
<p>Checking link  /<br />
adding index.htm<br />
adding photos.htm<br />
adding videos.htm<br />
adding projects.htm<br />
ignoring http://orderinchaos.davidwkennedy.com<br />
adding funstuff.htm<br />
adding about.htm<br />
ignoring http://twitter.com/davidwkennedy<br />
ignoring statcounter</p>
<p>Checking link  index.htm<br />
Checking link  photos.htm<br />
Checking link  videos.htm<br />
Checking link  projects.htm<br />
Checking link  funstuff.htm<br />
Checking link  about.htm<br />
done</p>
<p>dave@dave-sparta:~$</p>
<p>[/cc]</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://orderinchaos.org/2009/10/hide-email-addresses-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: Getting Time from Time Server</title>
		<link>http://orderinchaos.org/2009/09/c-getting-time-from-time-server/</link>
		<comments>http://orderinchaos.org/2009/09/c-getting-time-from-time-server/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 03:16:26 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nist]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[universal time]]></category>
		<category><![CDATA[utc]]></category>

		<guid isPermaLink="false">http://orderinchaos.org/?p=44</guid>
		<description><![CDATA[Here&#8217;s yet another old C# code snipped from my antiquated and dead blog. But throwin&#8217; this out there for anyone who might find it helpful.
[cc lang="csharp"  tab_size="2"]
try
{
   string newTime;
   System.Net.Sockets.TcpClient t =
      new System.Net.Sockets.TcpClient(&#8221;time-a.nist.gov&#8221;, 13);
   System.IO.StreamReader rd = new System.IO.StreamReader(t.GetStream());
   newTime [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s yet another old C# code snipped from my antiquated and dead blog. But throwin&#8217; this out there for anyone who might find it helpful.</p>
<p>[cc lang="csharp"  tab_size="2"]<br />
try<br />
{<br />
   string newTime;<br />
   System.Net.Sockets.TcpClient t =<br />
      new System.Net.Sockets.TcpClient(&#8221;time-a.nist.gov&#8221;, 13);<br />
   System.IO.StreamReader rd = new System.IO.StreamReader(t.GetStream());<br />
   newTime = rd.ReadToEnd();</p>
<p>   string[] times = newTime.Split(&#8217; &#8216;); // Parses The String<br />
   rd.Close();<br />
   t.Close();<br />
   Console.WriteLine(&#8221;Todays Date Is: &#8221; + times[1]);<br />
}<br />
[/cc]</p>
<p><span class="class1">I read somewhere that in order for this code to work, the TCP client has to be installed and enabled in Windows Vista. The TCP client is not, be default, enabled in Vista. To install it, Control Panel &#8212; Programs and Features &#8212; Turn Windows Features On or Off &#8212; TCP Client.</span></p>
<p>However, I tried this after unchecking my TCP client and it still worked &#8211; so maybe it&#8217;s just an old wive&#8217;s tale! I&#8217;d suggest trying it without the TCP client installed &#8211; but don&#8217;t count on it working if you&#8217;re planning on distributing this without further research.</p>
<p>The second line after the opening try { has &#8221; time-a.nist.gov&#8221;, which is the time server. You can swap this out with some of the other time servers below.If you were feeling ambitious, you might try creating a class that will try a random time server on it&#8217;s first try and then randomly try other time servers if the first <em>x</em> fail.</p>
<p><span class="class1"><strong>Name        |            IP              |              Location </strong><br />
time-a.nist.gov |  	129.6.15.28 | 	NIST, Gaithersburg, Maryland<br />
time-b.nist.gov |	129.6.15.29 |	NIST, Gaithersburg, Maryland<br />
time-a.timefreq.bldrdoc.gov| 	132.163.4.101 |	NIST, Boulder, Colorado<br />
time-b.timefreq.bldrdoc.gov |	132.163.4.102 |	NIST, Boulder, Colorado<br />
time-c.timefreq.bldrdoc.gov |	132.163.4.103 |	NIST, Boulder, Colorado<br />
utcnist.colorado.edu |	128.138.140.44 |	University of Colorado, Boulder<br />
time.nist.gov 	| 192.43.244.18  |	NCAR, Boulder, Colorado<br />
time-nw.nist.gov |	131.107.1.10 |	Microsoft, Redmond, Washington<br />
nist1.datum.com 	| 209.0.72.7  |	Datum, San Jose, California<br />
nist1.dc.certifiedtime.com |	216.200.93.8  |	Abovnet, Virginia<br />
nist1.nyc.certifiedtime.com |	208.184.49.9  |	Abovnet, New York City<br />
nist1.sjc.certifiedtime.com |	208.185.146.41 |	Abovnet, San Jose, California<br />
</span></p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://orderinchaos.org/2009/09/c-getting-time-from-time-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: Image.FromFile gives &#8220;Out of Memory Exception&#8221;</title>
		<link>http://orderinchaos.org/2009/09/c-imagefromfile-gives-out-of-memory-exception/</link>
		<comments>http://orderinchaos.org/2009/09/c-imagefromfile-gives-out-of-memory-exception/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 03:09:23 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[out of memory]]></category>

		<guid isPermaLink="false">http://orderinchaos.org/?p=42</guid>
		<description><![CDATA[This is an older post, from another [now dead] blog of mine, but I thought I&#8217;d throw it up here as well.
When working in C#, if you are trying to load a jpeg file and you use the System.Drawing.Image.FromFile method on a file that you do not have the proper permissions for, you will get [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>This is an older post, from another [now dead] blog of mine, but I thought I&#8217;d throw it up here as well.</p>
<p>When working in C#, if you are trying to load a jpeg file and you use the System.Drawing.Image.FromFile method on a file that you do not have the proper permissions for, you will get an &#8220;Out of Memory&#8221; exception. Somewhat misleading &#8211; but that is the error resulting from improper permissions.</p>
<p>I found a web site that claimed that you create a FileStream object from your file and then use the FromStream method on the Image object.</p>
<p>My solution for loading a file was as follows<br />
[cc lang="csharp"]<br />
private void addPhotoButton_Click(object sender, EventArgs e)<br />
{<br />
  DialogResult newPhoto;<br />
  Image newPhotoImage;<br />
  string newPhotoFileName;<br />
  newPhoto = openFileDialogAddPhoto.ShowDialog();<br />
  if(newPhoto == DialogResult.OK)<br />
  {<br />
      newPhotoFileName = openFileDialogAddPhoto.FileName;<br />
      FileStream photoStream =<br />
         new FileStream(newPhotoFileName,<br />
                              FileMode.Open, FileAccess.Read);<br />
      newPhotoImage = Image.FromStream(photoStream);<br />
   }<br />
}<br />
[/cc]</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://orderinchaos.org/2009/09/c-imagefromfile-gives-out-of-memory-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

