Projects

Ticket #424: performance_test.rb

File performance_test.rb, 6.6 KB (added by jordan.breeding@…, 2 years ago)
Line 
1#!/usr/bin/eval ruby
2
3require 'set'
4
5class JBBBinaryHeap
6    def initialize(isMaxHeap = false)
7        @mStorage = []
8        @mCompareProc = (isMaxHeap) ? lambda { |x, y| x < y } : lambda { |x, y| x > y }
9    end
10
11    def add(objectToAdd)
12        @mStorage.push(nil)
13        lastIndex = @mStorage.length - 1
14        while (lastIndex > 0)
15            parent = (lastIndex - 1) / 2
16            parentNode = @mStorage[parent]
17            break if (@mCompareProc.call(objectToAdd, parentNode))
18            @mStorage[lastIndex] = parentNode
19            lastIndex = parent
20        end
21        @mStorage[lastIndex] = objectToAdd
22    end
23
24    def remove
25        returnNode = @mStorage[0]
26        lastNode = @mStorage.pop
27        shiftDown(0, lastNode)
28        return returnNode
29    end
30
31    def min
32        return @mStorage[0]
33    end
34
35    def shiftDown(index, objectToShift)
36        return if @mStorage.empty?
37
38        while (index < (@mStorage.size / 2))
39            child = (2 * index) + 1
40       
41            child += 1 if ((child < (@mStorage.size - 1)) and @mCompareProc.call(@mStorage[child], @mStorage[child + 1]))
42
43            break unless (@mCompareProc.call(objectToShift, @mStorage[child]))
44
45            @mStorage[index] = @mStorage[child]
46            index = child
47        end
48        @mStorage[index] = objectToShift
49    end
50
51    def size
52        return @mStorage.size
53    end
54    alias length size
55
56    def empty?
57        return @mStorage.empty?
58    end
59
60    def to_s
61        return @mStorage.to_s
62    end
63end
64
65class JBBPQueue
66    def initialize(isMaxQueue = false)
67        @mObjs = JBBBinaryHeap.new(isMaxQueue)
68        @mObjsArray = []
69        @mHeapified = false
70    end
71
72    def buildHeap
73        return if (@mHeapified)
74
75        @mObjsArray.each { |item| @mObjs.add(item) }
76        @mObjsArray.clear
77        @mHeapified = true
78    end
79
80    def push(objectToAdd)
81        if (@mHeapified)
82            @mObjs.add(objectToAdd)
83        else
84            @mObjsArray.push(objectToAdd)
85        end
86    end
87
88    def pushObjects(objectsToAdd = [])
89        @mHeapified = false
90
91        @mObjsArray += objectsToAdd
92    end
93
94    def pop
95        return nil if empty?
96
97        buildHeap
98
99        return @mObjs.remove
100    end
101
102    def size
103        return @mObjs.size + @mObjsArray.size
104    end
105
106    def empty?
107        return (size == 0)
108    end
109
110    def to_s
111        buildHeap
112
113        return @mObjs.to_s
114    end
115end
116
117class String
118    def swap(first, second)
119        self.dup.swap!(first, second)
120    end
121   
122    def swap!(first, second)
123        self[first], self[second] = self[second], self[first]
124        return self
125    end
126   
127    def altSwap(first, second)
128        self.dup.altSwap!(first, second)
129    end
130   
131    def altSwap!(first, second)
132        temp_store = self[first]
133        self[first] = self[second]
134        self[second] = temp_store
135        return self
136    end
137end
138
139class JBBNode
140    include Comparable
141   
142    attr_reader :priority
143    attr_reader :level
144   
145    def initialize(priority = 0)
146        @priority = priority
147        @level = rand(5)
148        $globalCounter += 1
149    end
150   
151    def children
152        (1..5).collect { |offset|
153            newPriority = @priority + offset
154            JBBNode.new(newPriority) unless $globalSet.include?(newPriority)
155        }.compact
156    end
157   
158    def <=>(rhs)
159        return -1 if @level < rhs.level
160        return 1 if @level > rhs.level
161        return @priority <=> rhs.priority
162    end
163end
164
165$tempTime = Time.new
166
167# first test
168puts
169puts("Test 001 -- String character swap, 2500000 times")
170
171
172$staticString = "asdfghjkl;123456789"
173$firstTime = 0.0
174$secondTime = Time.new
1752500000.times {
176    randOne = rand($staticString.length)
177    randTwo = rand($staticString.length)
178    $tempTime = Time.new
179    newString = $staticString.swap(randOne, randTwo)
180    $firstTime += Time.new - $tempTime
181}
182$secondTime = Time.new - $secondTime
183
184puts("Test 001: %.4f seconds" % [$firstTime])
185puts("Overall: %.4f seconds" % [$secondTime])
186puts
187
188# second test
189puts
190puts("Test 002 -- String character swap (alternate), 2500000 times")
191
192$firstTime = 0.0
193$secondTime = Time.new
1942500000.times {
195    randOne = rand($staticString.length)
196    randTwo = rand($staticString.length)
197    $tempTime = Time.new
198    newString = $staticString.altSwap(randOne, randTwo)
199    $firstTime += Time.new - $tempTime
200}
201$secondTime = Time.new - $secondTime
202
203puts("Test 002: %.4f seconds" % [$firstTime])
204puts("Overall: %.4f seconds" % [$secondTime])
205puts
206
207# third and fourth tests
208puts
209puts("Test 003 and 004 -- Insert 500000 nodes into a priority queue using pop/push (003), plus Set calls (004)")
210
211$globalSet = Set.new
212$globalQueue = JBBPQueue.new
213$globalCounter = 0
214
215$firstTime = 0.0
216$secondTime = 0.0
217$thirdTime = Time.new
218
219$tempTime = Time.new
220$globalQueue.push(JBBNode.new)
221$firstTime += Time.new - $tempTime
222
223while (searchNode = $globalQueue.pop())
224    $firstTime += Time.new - $tempTime
225   
226    break unless ($globalCounter <= 500000)
227   
228    $tempTime = Time.new
229    if ($globalSet.include?(searchNode.priority))
230        $secondTime += Time.new - $tempTime
231        next
232    else
233        $secondTime += Time.new - $tempTime
234    end
235   
236    $tempTime = Time.new
237    $globalSet.add(searchNode.priority)
238    $secondTime += Time.new - $tempTime
239    searchNode.children.each { |item|
240        $tempTime = Time.new
241        $globalQueue.push(item)
242        $firstTime += Time.new - $tempTime
243    }
244end
245$thirdTime = Time.new - $thirdTime
246
247puts("Test 003: %.4f seconds" % [$firstTime])
248puts("Test 004: %.4f seconds" % [$secondTime])
249puts("Overall: %.4f seconds" % [$thirdTime])
250puts
251
252# fifth and sixth tests
253puts
254puts("Test 005 and 006 -- Insert 500000 nodes into a priority queue using pop/pushObjects (005), plus Set calls (006)")
255
256$globalSet = Set.new
257$globalQueue = JBBPQueue.new
258$globalCounter = 0
259
260$firstTime = 0.0
261$secondTime = 0.0
262$thirdTime = Time.new
263
264$tempTime = Time.new
265$globalQueue.push(JBBNode.new)
266$firstTime += Time.new - $tempTime
267
268while (searchNode = $globalQueue.pop())
269    $firstTime += Time.new - $tempTime
270   
271    break unless ($globalCounter <= 500000)
272   
273    $tempTime = Time.new
274    if ($globalSet.include?(searchNode.priority))
275        $secondTime += Time.new - $tempTime
276        next
277    else
278        $secondTime += Time.new - $tempTime
279    end
280   
281    $tempTime = Time.new
282    $globalSet.add(searchNode.priority)
283    $secondTime += Time.new - $tempTime
284   
285    $tempTime = Time.new
286    $globalQueue.pushObjects(searchNode.children)
287    $firstTime += Time.new - $tempTime
288end
289$thirdTime = Time.new - $thirdTime
290
291puts("Test 005: %.4f seconds" % [$firstTime])
292puts("Test 006: %.4f seconds" % [$secondTime])
293puts("Overall: %.4f seconds" % [$thirdTime])
294puts
295