|
25 | 25 | "import imghdr,struct,distutils.util,tempfile,time,string\n", |
26 | 26 | "from contextlib import contextmanager,ExitStack\n", |
27 | 27 | "from pdb import set_trace\n", |
28 | | - "from datetime import datetime, timezone" |
| 28 | + "from datetime import datetime, timezone\n", |
| 29 | + "from timeit import default_timer" |
29 | 30 | ] |
30 | 31 | }, |
31 | 32 | { |
|
36 | 37 | "source": [ |
37 | 38 | "from fastcore.test import *\n", |
38 | 39 | "from nbdev.showdoc import *\n", |
39 | | - "from fastcore.nb_imports import *" |
| 40 | + "from fastcore.nb_imports import *\n", |
| 41 | + "from time import sleep" |
40 | 42 | ] |
41 | 43 | }, |
42 | 44 | { |
|
376 | 378 | "text/markdown": [ |
377 | 379 | "<h4 id=\"ReindexCollection\" class=\"doc_header\"><code>class</code> <code>ReindexCollection</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
378 | 380 | "\n", |
379 | | - "> <code>ReindexCollection</code>(**`coll`**, **`idxs`**=*`None`*, **`cache`**=*`None`*, **`tfm`**=*`noop`*) :: [`GetAttr`](/foundation.html#GetAttr)\n", |
| 381 | + "> <code>ReindexCollection</code>(**`coll`**, **`idxs`**=*`None`*, **`cache`**=*`None`*, **`tfm`**=*`noop`*) :: [`GetAttr`](/basics.html#GetAttr)\n", |
380 | 382 | "\n", |
381 | 383 | "Reindexes collection `coll` with indices `idxs` and optional LRU cache of size `cache`" |
382 | 384 | ], |
|
616 | 618 | { |
617 | 619 | "data": { |
618 | 620 | "text/plain": [ |
619 | | - "['f', 'd', 'e', 'g', 'a', 'b', 'h', 'c']" |
| 621 | + "['d', 'h', 'g', 'a', 'c', 'f', 'e', 'b']" |
620 | 622 | ] |
621 | 623 | }, |
622 | 624 | "execution_count": null, |
|
1317 | 1319 | "## Other Helpers" |
1318 | 1320 | ] |
1319 | 1321 | }, |
| 1322 | + { |
| 1323 | + "cell_type": "code", |
| 1324 | + "execution_count": null, |
| 1325 | + "metadata": {}, |
| 1326 | + "outputs": [], |
| 1327 | + "source": [ |
| 1328 | + "#export\n", |
| 1329 | + "def time_events():\n", |
| 1330 | + " \"A simple event timer implemented as a coroutine\"\n", |
| 1331 | + " start,events = default_timer(),0\n", |
| 1332 | + " while True: events += (yield events,events/(default_timer()-start)) or 0" |
| 1333 | + ] |
| 1334 | + }, |
| 1335 | + { |
| 1336 | + "cell_type": "markdown", |
| 1337 | + "metadata": {}, |
| 1338 | + "source": [ |
| 1339 | + "This is convenient for tracking the frequency of events. Call `send(None)` to start the timer, and then 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." |
| 1340 | + ] |
| 1341 | + }, |
| 1342 | + { |
| 1343 | + "cell_type": "code", |
| 1344 | + "execution_count": null, |
| 1345 | + "metadata": {}, |
| 1346 | + "outputs": [ |
| 1347 | + { |
| 1348 | + "name": "stdout", |
| 1349 | + "output_type": "stream", |
| 1350 | + "text": [ |
| 1351 | + "# Events: 10, Freq/sec: 64.50\n" |
| 1352 | + ] |
| 1353 | + } |
| 1354 | + ], |
| 1355 | + "source": [ |
| 1356 | + "# Random wait function for testing `time_events`\n", |
| 1357 | + "def _randwait(): yield from (sleep(random.random()/30) for _ in range(10))\n", |
| 1358 | + "\n", |
| 1359 | + "c = time_events()\n", |
| 1360 | + "c.send(None) # Start timer by sending `None`\n", |
| 1361 | + "for o in _randwait(): c.send(1) # Send an event\n", |
| 1362 | + "events,freq = next(c) # `next` returns counter values\n", |
| 1363 | + "print(f'# Events: {events}, Freq/sec: {freq:.02f}')" |
| 1364 | + ] |
| 1365 | + }, |
1320 | 1366 | { |
1321 | 1367 | "cell_type": "code", |
1322 | 1368 | "execution_count": null, |
|
0 commit comments