]> git.imager.perl.org - poe-xs-queue-array.git/blob - bench1.perl
initial release
[poe-xs-queue-array.git] / bench1.perl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use blib;
5
6 $|=1;
7
8 use POE::Queue::Array;
9 use POE::XS::Queue::Array;
10
11 # The sequence length should be at least as many items as there are
12 # priorities.
13
14 sub MAX_PRIORITIES  () { 200 }
15 sub SEQUENCE_LENGTH () { 5000 }
16
17 die if SEQUENCE_LENGTH < MAX_PRIORITIES;
18
19 # Fisher-Yates shuffle them, for extra yummy randomness.  Use srand
20 # with the same seed each time so every @seq list represents different
21 # lengths of the same "random" sequence.
22
23 my @seq;
24 sub build_list {
25   my $priorities = shift;
26   my $factor = SEQUENCE_LENGTH / $priorities;
27
28   @seq = map { [ int($_ / $factor), $_ ] } (0..(SEQUENCE_LENGTH-1));
29
30   { srand(1);
31     my $i = @seq;
32     while (--$i) {
33       my $j = int rand($i+1);
34       @seq[$i,$j] = @seq[$j,$i];
35     }
36   }
37 }
38
39 # Run through the list for a number of benchmarks.  Each benchmark has
40 # a different number of priorities.
41
42 for my $priorities (1..MAX_PRIORITIES) {
43
44   build_list($priorities);
45
46   # One for each queue implementation.
47   for my $impl (qw(POE::Queue::Array POE::XS::Queue::Array)) {
48
49     my $queue = $impl->new();
50
51     ### Plain enqueue/dequeue.
52
53     my ($begin_usr, $begin_sys) = (times)[0,1];
54     $queue->enqueue(@$_) for @seq;
55     my ($cease_usr, $cease_sys) = (times)[0,1];
56
57     my $elapsed = ($cease_usr - $begin_usr) + ($cease_sys - $begin_sys);
58
59     print( join( "\t",
60                  $priorities,
61                  $impl, "enqueue-plain",
62                  $elapsed/SEQUENCE_LENGTH, # Time per operation.
63                ),
64            "\n"
65          );
66
67     ($begin_usr, $begin_sys) = (times)[0,1];
68     1 while $queue->dequeue_next;
69     ($cease_usr, $cease_sys) = (times)[0,1];
70
71     $elapsed = ($cease_usr - $begin_usr) + ($cease_sys - $begin_sys);
72
73     print( join( "\t",
74                  $priorities,
75                  $impl, "dequeue-plain",
76                  $elapsed/SEQUENCE_LENGTH, # Time per operation.
77                ),
78            "\n"
79          );
80
81     ### Next-priority enqueue/dequeue.  The enqueue is actually just a
82     ### plain one, but we get to see the effect of internal data
83     ### structure freeing tradeoffs.
84
85 #     ($begin_usr, $begin_sys) = (times)[0,1];
86 #     $queue->enqueue(@$_) for @seq;
87 #     ($cease_usr, $cease_sys) = (times)[0,1];
88
89 #     $elapsed = ($cease_usr - $begin_usr) + ($cease_sys - $begin_sys);
90
91 #     print( join( "\t",
92 #                  $priorities,
93 #                  $impl, "enqueue-np",
94 #                  $elapsed/SEQUENCE_LENGTH, # Time per operation.
95 #                ),
96 #            "\n"
97 #          );
98
99 #     ($begin_usr, $begin_sys) = (times)[0,1];
100 #     1 while scalar(@{$queue->dequeue_next_priority});
101 #     ($cease_usr, $cease_sys) = (times)[0,1];
102
103 #     $elapsed = ($cease_usr - $begin_usr) + ($cease_sys - $begin_sys);
104
105 #     print( join( "\t",
106 #                  $priorities,
107 #                  $impl, "dequeue-np",
108 #                  $elapsed/SEQUENCE_LENGTH, # Time per operation.
109 #                ),
110 #            "\n"
111 #          );
112   }
113 }