Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | from pybench import Test |
| 2 | |
| 3 | class PythonFunctionCalls(Test): |
| 4 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5 | version = 2.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | operations = 5*(1+4+4+2) |
| 7 | rounds = 60000 |
| 8 | |
| 9 | def test(self): |
| 10 | |
| 11 | global f,f1,g,h |
| 12 | |
| 13 | # define functions |
| 14 | def f(): |
| 15 | pass |
| 16 | |
| 17 | def f1(x): |
| 18 | pass |
| 19 | |
| 20 | def g(a,b,c): |
| 21 | return a,b,c |
| 22 | |
| 23 | def h(a,b,c,d=1,e=2,f=3): |
| 24 | return d,e,f |
| 25 | |
| 26 | # do calls |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 27 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | |
| 29 | f() |
| 30 | f1(i) |
| 31 | f1(i) |
| 32 | f1(i) |
| 33 | f1(i) |
| 34 | g(i,i,i) |
| 35 | g(i,i,i) |
| 36 | g(i,i,i) |
| 37 | g(i,i,i) |
| 38 | h(i,i,3,i,i) |
| 39 | h(i,i,i,2,i,3) |
| 40 | |
| 41 | f() |
| 42 | f1(i) |
| 43 | f1(i) |
| 44 | f1(i) |
| 45 | f1(i) |
| 46 | g(i,i,i) |
| 47 | g(i,i,i) |
| 48 | g(i,i,i) |
| 49 | g(i,i,i) |
| 50 | h(i,i,3,i,i) |
| 51 | h(i,i,i,2,i,3) |
| 52 | |
| 53 | f() |
| 54 | f1(i) |
| 55 | f1(i) |
| 56 | f1(i) |
| 57 | f1(i) |
| 58 | g(i,i,i) |
| 59 | g(i,i,i) |
| 60 | g(i,i,i) |
| 61 | g(i,i,i) |
| 62 | h(i,i,3,i,i) |
| 63 | h(i,i,i,2,i,3) |
| 64 | |
| 65 | f() |
| 66 | f1(i) |
| 67 | f1(i) |
| 68 | f1(i) |
| 69 | f1(i) |
| 70 | g(i,i,i) |
| 71 | g(i,i,i) |
| 72 | g(i,i,i) |
| 73 | g(i,i,i) |
| 74 | h(i,i,3,i,i) |
| 75 | h(i,i,i,2,i,3) |
| 76 | |
| 77 | f() |
| 78 | f1(i) |
| 79 | f1(i) |
| 80 | f1(i) |
| 81 | f1(i) |
| 82 | g(i,i,i) |
| 83 | g(i,i,i) |
| 84 | g(i,i,i) |
| 85 | g(i,i,i) |
| 86 | h(i,i,3,i,i) |
| 87 | h(i,i,i,2,i,3) |
| 88 | |
| 89 | def calibrate(self): |
| 90 | |
| 91 | global f,f1,g,h |
| 92 | |
| 93 | # define functions |
| 94 | def f(): |
| 95 | pass |
| 96 | |
| 97 | def f1(x): |
| 98 | pass |
| 99 | |
| 100 | def g(a,b,c): |
| 101 | return a,b,c |
| 102 | |
| 103 | def h(a,b,c,d=1,e=2,f=3): |
| 104 | return d,e,f |
| 105 | |
| 106 | # do calls |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 107 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | pass |
| 109 | |
| 110 | ### |
| 111 | |
Antoine Pitrou | 9a2310d | 2008-07-25 22:39:39 +0000 | [diff] [blame] | 112 | class ComplexPythonFunctionCalls(Test): |
| 113 | |
| 114 | version = 2.0 |
| 115 | operations = 4*5 |
| 116 | rounds = 100000 |
| 117 | |
| 118 | def test(self): |
| 119 | |
| 120 | # define functions |
| 121 | def f(a,b,c,d=1,e=2,f=3): |
| 122 | return f |
| 123 | |
| 124 | args = 1,2 |
| 125 | kwargs = dict(c=3,d=4,e=5) |
| 126 | |
| 127 | # do calls |
| 128 | for i in range(self.rounds): |
| 129 | f(a=i,b=i,c=i) |
| 130 | f(f=i,e=i,d=i,c=2,b=i,a=3) |
| 131 | f(1,b=i,**kwargs) |
| 132 | f(*args,**kwargs) |
| 133 | |
| 134 | f(a=i,b=i,c=i) |
| 135 | f(f=i,e=i,d=i,c=2,b=i,a=3) |
| 136 | f(1,b=i,**kwargs) |
| 137 | f(*args,**kwargs) |
| 138 | |
| 139 | f(a=i,b=i,c=i) |
| 140 | f(f=i,e=i,d=i,c=2,b=i,a=3) |
| 141 | f(1,b=i,**kwargs) |
| 142 | f(*args,**kwargs) |
| 143 | |
| 144 | f(a=i,b=i,c=i) |
| 145 | f(f=i,e=i,d=i,c=2,b=i,a=3) |
| 146 | f(1,b=i,**kwargs) |
| 147 | f(*args,**kwargs) |
| 148 | |
| 149 | f(a=i,b=i,c=i) |
| 150 | f(f=i,e=i,d=i,c=2,b=i,a=3) |
| 151 | f(1,b=i,**kwargs) |
| 152 | f(*args,**kwargs) |
| 153 | |
| 154 | |
| 155 | def calibrate(self): |
| 156 | |
| 157 | # define functions |
| 158 | def f(a,b,c,d=1,e=2,f=3): |
| 159 | return f |
| 160 | |
| 161 | args = 1,2 |
| 162 | kwargs = dict(c=3,d=4,e=5) |
| 163 | |
| 164 | # do calls |
| 165 | for i in range(self.rounds): |
| 166 | pass |
| 167 | |
| 168 | ### |
| 169 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 170 | class BuiltinFunctionCalls(Test): |
| 171 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | version = 2.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 173 | operations = 5*(2+5+5+5) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 174 | rounds = 60000 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 175 | |
| 176 | def test(self): |
| 177 | |
| 178 | # localize functions |
| 179 | f0 = globals |
| 180 | f1 = hash |
| 181 | f2 = cmp |
| 182 | f3 = range |
| 183 | |
| 184 | # do calls |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 185 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 186 | |
| 187 | f0() |
| 188 | f0() |
| 189 | f1(i) |
| 190 | f1(i) |
| 191 | f1(i) |
| 192 | f1(i) |
| 193 | f1(i) |
| 194 | f2(1,2) |
| 195 | f2(1,2) |
| 196 | f2(1,2) |
| 197 | f2(1,2) |
| 198 | f2(1,2) |
| 199 | f3(1,3,2) |
| 200 | f3(1,3,2) |
| 201 | f3(1,3,2) |
| 202 | f3(1,3,2) |
| 203 | f3(1,3,2) |
| 204 | |
| 205 | f0() |
| 206 | f0() |
| 207 | f1(i) |
| 208 | f1(i) |
| 209 | f1(i) |
| 210 | f1(i) |
| 211 | f1(i) |
| 212 | f2(1,2) |
| 213 | f2(1,2) |
| 214 | f2(1,2) |
| 215 | f2(1,2) |
| 216 | f2(1,2) |
| 217 | f3(1,3,2) |
| 218 | f3(1,3,2) |
| 219 | f3(1,3,2) |
| 220 | f3(1,3,2) |
| 221 | f3(1,3,2) |
| 222 | |
| 223 | f0() |
| 224 | f0() |
| 225 | f1(i) |
| 226 | f1(i) |
| 227 | f1(i) |
| 228 | f1(i) |
| 229 | f1(i) |
| 230 | f2(1,2) |
| 231 | f2(1,2) |
| 232 | f2(1,2) |
| 233 | f2(1,2) |
| 234 | f2(1,2) |
| 235 | f3(1,3,2) |
| 236 | f3(1,3,2) |
| 237 | f3(1,3,2) |
| 238 | f3(1,3,2) |
| 239 | f3(1,3,2) |
| 240 | |
| 241 | f0() |
| 242 | f0() |
| 243 | f1(i) |
| 244 | f1(i) |
| 245 | f1(i) |
| 246 | f1(i) |
| 247 | f1(i) |
| 248 | f2(1,2) |
| 249 | f2(1,2) |
| 250 | f2(1,2) |
| 251 | f2(1,2) |
| 252 | f2(1,2) |
| 253 | f3(1,3,2) |
| 254 | f3(1,3,2) |
| 255 | f3(1,3,2) |
| 256 | f3(1,3,2) |
| 257 | f3(1,3,2) |
| 258 | |
| 259 | f0() |
| 260 | f0() |
| 261 | f1(i) |
| 262 | f1(i) |
| 263 | f1(i) |
| 264 | f1(i) |
| 265 | f1(i) |
| 266 | f2(1,2) |
| 267 | f2(1,2) |
| 268 | f2(1,2) |
| 269 | f2(1,2) |
| 270 | f2(1,2) |
| 271 | f3(1,3,2) |
| 272 | f3(1,3,2) |
| 273 | f3(1,3,2) |
| 274 | f3(1,3,2) |
| 275 | f3(1,3,2) |
| 276 | |
| 277 | def calibrate(self): |
| 278 | |
| 279 | # localize functions |
| 280 | f0 = dir |
| 281 | f1 = hash |
| 282 | f2 = range |
| 283 | f3 = range |
| 284 | |
| 285 | # do calls |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 286 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | pass |
| 288 | |
| 289 | ### |
| 290 | |
| 291 | class PythonMethodCalls(Test): |
| 292 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 293 | version = 2.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 294 | operations = 5*(6 + 5 + 4) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 295 | rounds = 30000 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 296 | |
| 297 | def test(self): |
| 298 | |
| 299 | class c: |
| 300 | |
| 301 | x = 2 |
| 302 | s = 'string' |
| 303 | |
| 304 | def f(self): |
| 305 | |
| 306 | return self.x |
| 307 | |
| 308 | def j(self,a,b): |
| 309 | |
| 310 | self.y = a |
| 311 | self.t = b |
| 312 | return self.y |
| 313 | |
| 314 | def k(self,a,b,c=3): |
| 315 | |
| 316 | self.y = a |
| 317 | self.s = b |
| 318 | self.t = c |
| 319 | |
| 320 | o = c() |
| 321 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 322 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | |
| 324 | o.f() |
| 325 | o.f() |
| 326 | o.f() |
| 327 | o.f() |
| 328 | o.f() |
| 329 | o.f() |
| 330 | o.j(i,i) |
| 331 | o.j(i,i) |
| 332 | o.j(i,2) |
| 333 | o.j(i,2) |
| 334 | o.j(2,2) |
| 335 | o.k(i,i) |
| 336 | o.k(i,2) |
| 337 | o.k(i,2,3) |
| 338 | o.k(i,i,c=4) |
| 339 | |
| 340 | o.f() |
| 341 | o.f() |
| 342 | o.f() |
| 343 | o.f() |
| 344 | o.f() |
| 345 | o.f() |
| 346 | o.j(i,i) |
| 347 | o.j(i,i) |
| 348 | o.j(i,2) |
| 349 | o.j(i,2) |
| 350 | o.j(2,2) |
| 351 | o.k(i,i) |
| 352 | o.k(i,2) |
| 353 | o.k(i,2,3) |
| 354 | o.k(i,i,c=4) |
| 355 | |
| 356 | o.f() |
| 357 | o.f() |
| 358 | o.f() |
| 359 | o.f() |
| 360 | o.f() |
| 361 | o.f() |
| 362 | o.j(i,i) |
| 363 | o.j(i,i) |
| 364 | o.j(i,2) |
| 365 | o.j(i,2) |
| 366 | o.j(2,2) |
| 367 | o.k(i,i) |
| 368 | o.k(i,2) |
| 369 | o.k(i,2,3) |
| 370 | o.k(i,i,c=4) |
| 371 | |
| 372 | o.f() |
| 373 | o.f() |
| 374 | o.f() |
| 375 | o.f() |
| 376 | o.f() |
| 377 | o.f() |
| 378 | o.j(i,i) |
| 379 | o.j(i,i) |
| 380 | o.j(i,2) |
| 381 | o.j(i,2) |
| 382 | o.j(2,2) |
| 383 | o.k(i,i) |
| 384 | o.k(i,2) |
| 385 | o.k(i,2,3) |
| 386 | o.k(i,i,c=4) |
| 387 | |
| 388 | o.f() |
| 389 | o.f() |
| 390 | o.f() |
| 391 | o.f() |
| 392 | o.f() |
| 393 | o.f() |
| 394 | o.j(i,i) |
| 395 | o.j(i,i) |
| 396 | o.j(i,2) |
| 397 | o.j(i,2) |
| 398 | o.j(2,2) |
| 399 | o.k(i,i) |
| 400 | o.k(i,2) |
| 401 | o.k(i,2,3) |
| 402 | o.k(i,i,c=4) |
| 403 | |
| 404 | def calibrate(self): |
| 405 | |
| 406 | class c: |
| 407 | |
| 408 | x = 2 |
| 409 | s = 'string' |
| 410 | |
| 411 | def f(self): |
| 412 | |
| 413 | return self.x |
| 414 | |
| 415 | def j(self,a,b): |
| 416 | |
| 417 | self.y = a |
| 418 | self.t = b |
| 419 | |
| 420 | def k(self,a,b,c=3): |
| 421 | |
| 422 | self.y = a |
| 423 | self.s = b |
| 424 | self.t = c |
| 425 | |
| 426 | o = c |
| 427 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 428 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 429 | pass |
| 430 | |
| 431 | ### |
| 432 | |
| 433 | class Recursion(Test): |
| 434 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 435 | version = 2.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 436 | operations = 5 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 437 | rounds = 100000 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | |
| 439 | def test(self): |
| 440 | |
| 441 | global f |
| 442 | |
| 443 | def f(x): |
| 444 | |
| 445 | if x > 1: |
| 446 | return f(x-1) |
| 447 | return 1 |
| 448 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 449 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 450 | f(10) |
| 451 | f(10) |
| 452 | f(10) |
| 453 | f(10) |
| 454 | f(10) |
| 455 | |
| 456 | def calibrate(self): |
| 457 | |
| 458 | global f |
| 459 | |
| 460 | def f(x): |
| 461 | |
| 462 | if x > 0: |
| 463 | return f(x-1) |
| 464 | return 1 |
| 465 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 466 | for i in range(self.rounds): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 467 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 468 | |
| 469 | |
| 470 | ### Test to make Fredrik happy... |
| 471 | |
| 472 | if __name__ == '__main__': |
| 473 | import timeit |
| 474 | if 0: |
| 475 | timeit.TestClass = PythonFunctionCalls |
| 476 | timeit.main(['-s', 'test = TestClass(); test.rounds = 1000', |
| 477 | 'test.test()']) |
| 478 | else: |
| 479 | setup = """\ |
| 480 | global f,f1,g,h |
| 481 | |
| 482 | # define functions |
| 483 | def f(): |
| 484 | pass |
| 485 | |
| 486 | def f1(x): |
| 487 | pass |
| 488 | |
| 489 | def g(a,b,c): |
| 490 | return a,b,c |
| 491 | |
| 492 | def h(a,b,c,d=1,e=2,f=3): |
| 493 | return d,e,f |
| 494 | |
| 495 | i = 1 |
| 496 | """ |
| 497 | test = """\ |
| 498 | f() |
| 499 | f1(i) |
| 500 | f1(i) |
| 501 | f1(i) |
| 502 | f1(i) |
| 503 | g(i,i,i) |
| 504 | g(i,i,i) |
| 505 | g(i,i,i) |
| 506 | g(i,i,i) |
| 507 | h(i,i,3,i,i) |
| 508 | h(i,i,i,2,i,3) |
| 509 | |
| 510 | f() |
| 511 | f1(i) |
| 512 | f1(i) |
| 513 | f1(i) |
| 514 | f1(i) |
| 515 | g(i,i,i) |
| 516 | g(i,i,i) |
| 517 | g(i,i,i) |
| 518 | g(i,i,i) |
| 519 | h(i,i,3,i,i) |
| 520 | h(i,i,i,2,i,3) |
| 521 | |
| 522 | f() |
| 523 | f1(i) |
| 524 | f1(i) |
| 525 | f1(i) |
| 526 | f1(i) |
| 527 | g(i,i,i) |
| 528 | g(i,i,i) |
| 529 | g(i,i,i) |
| 530 | g(i,i,i) |
| 531 | h(i,i,3,i,i) |
| 532 | h(i,i,i,2,i,3) |
| 533 | |
| 534 | f() |
| 535 | f1(i) |
| 536 | f1(i) |
| 537 | f1(i) |
| 538 | f1(i) |
| 539 | g(i,i,i) |
| 540 | g(i,i,i) |
| 541 | g(i,i,i) |
| 542 | g(i,i,i) |
| 543 | h(i,i,3,i,i) |
| 544 | h(i,i,i,2,i,3) |
| 545 | |
| 546 | f() |
| 547 | f1(i) |
| 548 | f1(i) |
| 549 | f1(i) |
| 550 | f1(i) |
| 551 | g(i,i,i) |
| 552 | g(i,i,i) |
| 553 | g(i,i,i) |
| 554 | g(i,i,i) |
| 555 | h(i,i,3,i,i) |
| 556 | h(i,i,i,2,i,3) |
| 557 | """ |
| 558 | |
| 559 | timeit.main(['-s', setup, |
| 560 | test]) |