queue_perf.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2014 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <!--
  9. -->
  10. <head>
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <title>Closure Performance Tests - goog.structs.Queue</title>
  13. <link rel="stylesheet" type="text/css" href="../testing/performancetable.css"/>
  14. <script src="../base.js"></script>
  15. <script>
  16. goog.require('goog.functions');
  17. goog.require('goog.structs.Queue');
  18. goog.require('goog.testing.PerformanceTable');
  19. goog.require('goog.testing.jsunit');
  20. </script>
  21. </head>
  22. <body>
  23. <h1>goog.structs.Queue Performance Tests</h1>
  24. <p>
  25. <strong>User-agent:</strong>
  26. <script>document.write(navigator.userAgent);</script>
  27. </p>
  28. <div id="perfTable"></div>
  29. <hr>
  30. <script>
  31. var table = new goog.testing.PerformanceTable(
  32. goog.dom.getElement('perfTable'));
  33. // Number of operations to measure in each table line.
  34. var OPS_COUNT = 10000;
  35. var smallSizes = [1, 2, 5, 10, 20, 50, 100, 1000, 10000];
  36. var largeSizes = [100000];
  37. function populateQueue(size) {
  38. var q = new goog.structs.Queue();
  39. for (var i = 0; i < size; ++i) {
  40. q.enqueue(i);
  41. }
  42. return q;
  43. }
  44. function enqueueDequeueTest(size) {
  45. var q = populateQueue(size);
  46. table.run(function() {
  47. for (var i = 0; i < OPS_COUNT; ++i) {
  48. q.dequeue();
  49. q.enqueue(i);
  50. }
  51. }, 'Enqueue and dequeue, size ' + size);
  52. }
  53. function containsTest(size) {
  54. var q = populateQueue(size);
  55. table.run(function() {
  56. for (var i = 0; i < OPS_COUNT; ++i) {
  57. q.contains(i);
  58. }
  59. }, 'Contains every element, size ' + size);
  60. }
  61. function removeTest(size) {
  62. var q = populateQueue(size);
  63. if (size == 1) {
  64. return;
  65. }
  66. table.run(function() {
  67. var offset = Math.round(size / 2);
  68. for (var i = 0; i < OPS_COUNT; ++i) {
  69. q.remove(i + offset);
  70. q.enqueue(i + size);
  71. }
  72. }, 'Remove element from the middle, size ' + size);
  73. }
  74. function getValuesTest(size) {
  75. var q = populateQueue(size);
  76. table.run(function() {
  77. for (var i = 0; i < OPS_COUNT; ++i) {
  78. q.getValues()[size - 1];
  79. }
  80. }, 'Get values, size ' + size);
  81. }
  82. function testEnqueueDequeueSmall() {
  83. goog.array.forEach(smallSizes, enqueueDequeueTest);
  84. }
  85. function testEnqueueDequeueLarge() {
  86. goog.array.forEach(largeSizes, enqueueDequeueTest);
  87. }
  88. function testContainsSmall() {
  89. goog.array.forEach(smallSizes, containsTest);
  90. }
  91. function testContainsLarge() {
  92. goog.array.forEach(largeSizes, containsTest);
  93. }
  94. function testRemoveSmall() {
  95. goog.array.forEach(smallSizes, removeTest);
  96. }
  97. function testRemoveLarge() {
  98. goog.array.forEach(largeSizes, removeTest);
  99. }
  100. function testGetValuesSmall() {
  101. goog.array.forEach(smallSizes, getValuesTest);
  102. }
  103. function testGetValuesLarge() {
  104. goog.array.forEach(largeSizes, getValuesTest);
  105. }
  106. </script>
  107. </body>
  108. </html>