access-nsieve.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright (C) 2007 Apple Inc. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  12. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  15. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  16. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  19. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  21. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. if(typeof(WScript) === "undefined")
  24. {
  25. var WScript = {
  26. Echo: print
  27. }
  28. }
  29. function record(time) {
  30. document.getElementById("console").innerHTML = time + "ms";
  31. if (window.parent) {
  32. parent.recordResult(time);
  33. }
  34. }
  35. var _sunSpiderStartDate = new Date();
  36. // The Great Computer Language Shootout
  37. // http://shootout.alioth.debian.org/
  38. //
  39. // modified by Isaac Gouy
  40. function pad(number,width){
  41. var s = number.toString();
  42. var prefixWidth = width - s.length;
  43. if (prefixWidth>0){
  44. for (var i=1; i<=prefixWidth; i++) s = " " + s;
  45. }
  46. return s;
  47. }
  48. function nsieve(m, isPrime){
  49. var i, k, count;
  50. for (i=2; i<=m; i++) { isPrime[i] = true; }
  51. count = 0;
  52. for (i=2; i<=m; i++){
  53. if (isPrime[i]) {
  54. for (k=i+i; k<=m; k+=i) isPrime[k] = false;
  55. count++;
  56. }
  57. }
  58. return count;
  59. }
  60. function sieve() {
  61. var sum = 0;
  62. for (var i = 1; i <= 3; i++ ) {
  63. var m = (1<<i)*10000;
  64. var flags = Array(m+1);
  65. sum += nsieve(m, flags);
  66. }
  67. return sum;
  68. }
  69. var result = sieve();
  70. var expected = 14302;
  71. if (result != expected)
  72. throw "ERROR: bad result: expected " + expected + " but got " + result;
  73. var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
  74. WScript.Echo("### TIME:", _sunSpiderInterval, "ms");