1from pybench import Test 2 3class SimpleListManipulation(Test): 4 5 version = 2.0 6 operations = 5* (6 + 6 + 6) 7 rounds = 130000 8 9 def test(self): 10 11 l = [] 12 append = l.append 13 14 for i in xrange(self.rounds): 15 16 append(2) 17 append(3) 18 append(4) 19 append(2) 20 append(3) 21 append(4) 22 23 l[0] = 3 24 l[1] = 4 25 l[2] = 5 26 l[3] = 3 27 l[4] = 4 28 l[5] = 5 29 30 x = l[0] 31 x = l[1] 32 x = l[2] 33 x = l[3] 34 x = l[4] 35 x = l[5] 36 37 append(2) 38 append(3) 39 append(4) 40 append(2) 41 append(3) 42 append(4) 43 44 l[0] = 3 45 l[1] = 4 46 l[2] = 5 47 l[3] = 3 48 l[4] = 4 49 l[5] = 5 50 51 x = l[0] 52 x = l[1] 53 x = l[2] 54 x = l[3] 55 x = l[4] 56 x = l[5] 57 58 append(2) 59 append(3) 60 append(4) 61 append(2) 62 append(3) 63 append(4) 64 65 l[0] = 3 66 l[1] = 4 67 l[2] = 5 68 l[3] = 3 69 l[4] = 4 70 l[5] = 5 71 72 x = l[0] 73 x = l[1] 74 x = l[2] 75 x = l[3] 76 x = l[4] 77 x = l[5] 78 79 append(2) 80 append(3) 81 append(4) 82 append(2) 83 append(3) 84 append(4) 85 86 l[0] = 3 87 l[1] = 4 88 l[2] = 5 89 l[3] = 3 90 l[4] = 4 91 l[5] = 5 92 93 x = l[0] 94 x = l[1] 95 x = l[2] 96 x = l[3] 97 x = l[4] 98 x = l[5] 99 100 append(2) 101 append(3) 102 append(4) 103 append(2) 104 append(3) 105 append(4) 106 107 l[0] = 3 108 l[1] = 4 109 l[2] = 5 110 l[3] = 3 111 l[4] = 4 112 l[5] = 5 113 114 x = l[0] 115 x = l[1] 116 x = l[2] 117 x = l[3] 118 x = l[4] 119 x = l[5] 120 121 if len(l) > 10000: 122 # cut down the size 123 del l[:] 124 125 def calibrate(self): 126 127 l = [] 128 append = l.append 129 130 for i in xrange(self.rounds): 131 pass 132 133class ListSlicing(Test): 134 135 version = 2.0 136 operations = 25*(3+1+2+1) 137 rounds = 800 138 139 def test(self): 140 141 n = range(100) 142 r = range(25) 143 144 for i in xrange(self.rounds): 145 146 l = n[:] 147 148 for j in r: 149 150 m = l[50:] 151 m = l[:25] 152 m = l[50:55] 153 l[:3] = n 154 m = l[:-1] 155 m = l[1:] 156 l[-1:] = n 157 158 def calibrate(self): 159 160 n = range(100) 161 r = range(25) 162 163 for i in xrange(self.rounds): 164 for j in r: 165 pass 166 167class SmallLists(Test): 168 169 version = 2.0 170 operations = 5*(1+ 6 + 6 + 3 + 1) 171 rounds = 80000 172 173 def test(self): 174 175 for i in xrange(self.rounds): 176 177 l = [] 178 179 append = l.append 180 append(2) 181 append(3) 182 append(4) 183 append(2) 184 append(3) 185 append(4) 186 187 l[0] = 3 188 l[1] = 4 189 l[2] = 5 190 l[3] = 3 191 l[4] = 4 192 l[5] = 5 193 194 l[:3] = [1,2,3] 195 m = l[:-1] 196 m = l[1:] 197 198 l[-1:] = [4,5,6] 199 200 l = [] 201 202 append = l.append 203 append(2) 204 append(3) 205 append(4) 206 append(2) 207 append(3) 208 append(4) 209 210 l[0] = 3 211 l[1] = 4 212 l[2] = 5 213 l[3] = 3 214 l[4] = 4 215 l[5] = 5 216 217 l[:3] = [1,2,3] 218 m = l[:-1] 219 m = l[1:] 220 221 l[-1:] = [4,5,6] 222 223 l = [] 224 225 append = l.append 226 append(2) 227 append(3) 228 append(4) 229 append(2) 230 append(3) 231 append(4) 232 233 l[0] = 3 234 l[1] = 4 235 l[2] = 5 236 l[3] = 3 237 l[4] = 4 238 l[5] = 5 239 240 l[:3] = [1,2,3] 241 m = l[:-1] 242 m = l[1:] 243 244 l[-1:] = [4,5,6] 245 246 l = [] 247 248 append = l.append 249 append(2) 250 append(3) 251 append(4) 252 append(2) 253 append(3) 254 append(4) 255 256 l[0] = 3 257 l[1] = 4 258 l[2] = 5 259 l[3] = 3 260 l[4] = 4 261 l[5] = 5 262 263 l[:3] = [1,2,3] 264 m = l[:-1] 265 m = l[1:] 266 267 l[-1:] = [4,5,6] 268 269 l = [] 270 271 append = l.append 272 append(2) 273 append(3) 274 append(4) 275 append(2) 276 append(3) 277 append(4) 278 279 l[0] = 3 280 l[1] = 4 281 l[2] = 5 282 l[3] = 3 283 l[4] = 4 284 l[5] = 5 285 286 l[:3] = [1,2,3] 287 m = l[:-1] 288 m = l[1:] 289 290 l[-1:] = [4,5,6] 291 292 def calibrate(self): 293 294 for i in xrange(self.rounds): 295 pass 296 297class SimpleListComprehensions(Test): 298 299 version = 2.0 300 operations = 6 301 rounds = 20000 302 303 def test(self): 304 305 n = range(10) * 10 306 307 for i in xrange(self.rounds): 308 l = [x for x in n] 309 l = [x for x in n if x] 310 l = [x for x in n if not x] 311 312 l = [x for x in n] 313 l = [x for x in n if x] 314 l = [x for x in n if not x] 315 316 def calibrate(self): 317 318 n = range(10) * 10 319 320 for i in xrange(self.rounds): 321 pass 322 323class NestedListComprehensions(Test): 324 325 version = 2.0 326 operations = 6 327 rounds = 20000 328 329 def test(self): 330 331 m = range(10) 332 n = range(10) 333 334 for i in xrange(self.rounds): 335 l = [x for x in n for y in m] 336 l = [y for x in n for y in m] 337 338 l = [x for x in n for y in m if y] 339 l = [y for x in n for y in m if x] 340 341 l = [x for x in n for y in m if not y] 342 l = [y for x in n for y in m if not x] 343 344 def calibrate(self): 345 346 m = range(10) 347 n = range(10) 348 349 for i in xrange(self.rounds): 350 pass 351