|
618 | 618 | { |
619 | 619 | "data": { |
620 | 620 | "text/plain": [ |
621 | | - "['a', 'e', 'h', 'b', 'g', 'd', 'c', 'f']" |
| 621 | + "['a', 'f', 'b', 'g', 'd', 'e', 'h', 'c']" |
622 | 622 | ] |
623 | 623 | }, |
624 | 624 | "execution_count": null, |
|
1421 | 1421 | " return f" |
1422 | 1422 | ] |
1423 | 1423 | }, |
1424 | | - { |
1425 | | - "cell_type": "code", |
1426 | | - "execution_count": null, |
1427 | | - "metadata": {}, |
1428 | | - "outputs": [], |
1429 | | - "source": [ |
1430 | | - "#export\n", |
1431 | | - "@autostart\n", |
1432 | | - "def time_events():\n", |
1433 | | - " \"An event timer implemented as a coroutine\"\n", |
1434 | | - " start,events = default_timer(),0\n", |
1435 | | - " while True: events += (yield events,events/(default_timer()-start)) or 0" |
1436 | | - ] |
1437 | | - }, |
1438 | | - { |
1439 | | - "cell_type": "markdown", |
1440 | | - "metadata": {}, |
1441 | | - "source": [ |
1442 | | - "This is convenient for tracking the frequency of events. Call `send(n)` any time you want to add `n` events to the counter. Pass the object to `next()` to get a tuple of the number of events and the frequency/second." |
1443 | | - ] |
1444 | | - }, |
1445 | | - { |
1446 | | - "cell_type": "code", |
1447 | | - "execution_count": null, |
1448 | | - "metadata": {}, |
1449 | | - "outputs": [], |
1450 | | - "source": [ |
1451 | | - "# Random wait function for testing `time_events`\n", |
1452 | | - "def _randwait(): yield from (sleep(random.random()/200) for _ in range(100))" |
1453 | | - ] |
1454 | | - }, |
1455 | | - { |
1456 | | - "cell_type": "code", |
1457 | | - "execution_count": null, |
1458 | | - "metadata": {}, |
1459 | | - "outputs": [ |
1460 | | - { |
1461 | | - "name": "stdout", |
1462 | | - "output_type": "stream", |
1463 | | - "text": [ |
1464 | | - "# Events: 100, Freq/sec: 396.51\n" |
1465 | | - ] |
1466 | | - } |
1467 | | - ], |
1468 | | - "source": [ |
1469 | | - "c = time_events() # Start timer\n", |
1470 | | - "for o in _randwait(): c.send(1) # Send an event\n", |
1471 | | - "events,freq = next(c) # Return counter values\n", |
1472 | | - "c.close() # Close when done\n", |
1473 | | - "print(f'# Events: {events}, Freq/sec: {freq:.02f}')" |
1474 | | - ] |
1475 | | - }, |
1476 | 1424 | { |
1477 | 1425 | "cell_type": "code", |
1478 | 1426 | "execution_count": null, |
|
1544 | 1492 | "name": "stdout", |
1545 | 1493 | "output_type": "stream", |
1546 | 1494 | "text": [ |
1547 | | - "Num Events: 5, Freq/sec: 646.7\n", |
1548 | | - "Most recent: ▁▇▃▇▆ 259.5 450.4 362.1 441.0 427.9\n" |
| 1495 | + "Num Events: 1, Freq/sec: 225.7\n", |
| 1496 | + "Most recent: ▇▁▁▁▁ 346.2 331.6 334.4 332.2 332.9\n" |
1549 | 1497 | ] |
1550 | 1498 | } |
1551 | 1499 | ], |
1552 | 1500 | "source": [ |
| 1501 | + "# Random wait function for testing\n", |
| 1502 | + "def _randwait(): yield from (sleep(random.random()/200) for _ in range(100))\n", |
| 1503 | + "\n", |
1553 | 1504 | "c = EventTimer(store=5, span=0.03)\n", |
1554 | 1505 | "for o in _randwait(): c.add(1)\n", |
1555 | 1506 | "print(f'Num Events: {c.events}, Freq/sec: {c.freq:.01f}')\n", |
|
0 commit comments