{"id":677,"date":"2008-06-15T17:08:51","date_gmt":"2008-06-15T16:08:51","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?page_id=677"},"modified":"2008-06-15T17:08:51","modified_gmt":"2008-06-15T16:08:51","slug":"counting-primes-in-flash-and-silverlight","status":"publish","type":"page","link":"https:\/\/www.itwriting.com\/blog\/articles\/counting-primes-in-flash-and-silverlight","title":{"rendered":"Counting Primes in Flash and Silverlight"},"content":{"rendered":"<p>I&#8217;m always interested in performance comparisons. In the RIA world, two that you may have seen are <a href=\"http:\/\/bubblemark.com\/\" target=\"_blank\">Bubblemark<\/a> and more recently <a href=\"http:\/\/www.craftymind.com\/2008\/05\/22\/introducing-guimark-an-ria-benchmark-for-flex-silverlight-html-and-more\/\" target=\"_blank\">GUIMark<\/a>. Those tests look at graphics; I thought it would be fun to look at some tight code in a loop.<\/p>\n<p>You can <a href=\"http:\/\/www.itwriting.com\/primetest\/index.html\">try the test here<\/a>.<\/p>\n<p>The code is below. If I&#8217;ve done something that tilts the test in favour of one or the other runtime, let me know.<\/p>\n<p>Please don\u2019t take this too seriously. I may have messed up the test; it is only one small aspect of performance; and there are lots of other factors to think about. I just find this sort of thing interesting.<\/p>\n<p><strong>ActionScript code:<\/strong><\/p>\n<pre>public function countprimes():void {\nvar n:int = int(testnum.text);\nvar start: Date = new Date;\nvar i: int;\nvar j: int;\nvar numprimes: int;\nvar limit: Number;\nnumprimes = 1; \/\/ 2 is prime\n\nfor (i = 3; i&lt;= n; i+=2)\n{\nvar isPrime: Boolean = true;\n\nlimit = Math.ceil(Math.sqrt(i)) + 1;\n\nfor (j = 3; j &lt; limit; j+=2)\n{\nif (i % j == 0)\n{\nisPrime = false;\nbreak;\n}\n\nif (isPrime != true)\n{\ncontinue;\n}\n}\n\nif (isPrime)\n{\nnumprimes ++;\n}\n}\n\nvar end: Date = new Date;\n\nvar timetaken:Number = Number(int(end) - int(start));\ntimetaken = timetaken \/ 1000;\n\nlbresult.text = \"Number of primes up to: \" + n + \" is \" + numprimes + \", counted in \" + timetaken + \" secs.\";\n}<\/pre>\n<p><strong>C# Code:<\/strong><\/p>\n<pre>void btnCount_Click(object sender, RoutedEventArgs e)\n{\nint n = Int32.Parse(this.txtMaxNum.Text);\nDateTime start = DateTime.Now;\nint i;\nint j;\nint numprimes;\ndouble limit;\nnumprimes = 1; \/\/2 is prime\n\nfor (i = 3; i&lt;= n; i+=2)\n{\nbool isPrime = true;\n\nlimit = Math.Ceiling(Math.Sqrt(i)) + 1;\n\nfor (j = 3; j &lt; limit; j+=2)\n{\nif (i % j == 0)\n{\nisPrime = false;\nbreak;\n}\n\nif (isPrime != true)\n{\ncontinue;\n}\n}\n\nif (isPrime)\n{\nnumprimes ++;\n}\n}\n\nDateTime end = DateTime.Now;\n\nDouble timetaken = end.Ticks - start.Ticks;\ntimetaken = timetaken \/ 10000000;\n\nthis.lbMessage.Text = \"Number of primes up to: \" + n + \" is \" + numprimes + \", counted in \" + timetaken + \" secs.\";\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m always interested in performance comparisons. In the RIA world, two that you may have seen are Bubblemark and more recently GUIMark. Those tests look at graphics; I thought it would be fun to look at some tight code in a loop. You can try the test here. The code is below. If I&#8217;ve done &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/articles\/counting-primes-in-flash-and-silverlight\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Counting Primes in Flash and Silverlight<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2036,"menu_order":270,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-677","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/pages\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/pages\/677\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/pages\/2036"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}