Skip to content

Commit c057ff3

Browse files
committed
is int
1 parent 94577a5 commit c057ff3

File tree

1 file changed

+179
-1
lines changed

1 file changed

+179
-1
lines changed

benchmarks/timeit_tests.ipynb

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:57df5708c2fce146df742136edf2eebb38c32146ab9e612bfd303a086b5bf7d9"
4+
"signature": "sha256:5f459d4c7e1521c71838da53736d2fbffd43b39d57d8b653cde1ec9333346574"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -18,6 +18,17 @@
1818
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb)"
1919
]
2020
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"<hr>\n",
26+
"I am really looking forward to your comments and suggestions to improve and extend this collection! Just send me a quick note \n",
27+
"via Twitter: [@rasbt](https://twitter.com/rasbt) \n",
28+
"or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)\n",
29+
"<hr>"
30+
]
31+
},
2132
{
2233
"cell_type": "markdown",
2334
"metadata": {},
@@ -37,6 +48,8 @@
3748
" - [String reversing: [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
3849
" - [String concatenation: `+=` vs. `''.join()`](#string_concat)\n",
3950
" - [Assembling strings](#string_assembly) \n",
51+
" - [Testing if a string is an integer](#is_integer)\n",
52+
" - [Testing if a string is a number](#is_number)\n",
4053
"- [List operations](#list_operations)\n",
4154
" - [List reversing: [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
4255
" - [Creating lists using conditional statements](#create_cond_list)\n",
@@ -229,6 +242,8 @@
229242
"cell_type": "markdown",
230243
"metadata": {},
231244
"source": [
245+
"<br>\n",
246+
"<br>\n",
232247
"<a name='string_assembly'></a>\n",
233248
"## Assembling strings\n",
234249
"\n",
@@ -294,6 +309,161 @@
294309
"cell_type": "markdown",
295310
"metadata": {},
296311
"source": [
312+
"<br>\n",
313+
"<br>\n",
314+
"<a name='is_integer'></a>\n",
315+
"## Testing if a string is an integer"
316+
]
317+
},
318+
{
319+
"cell_type": "code",
320+
"collapsed": false,
321+
"input": [
322+
"import timeit\n",
323+
"\n",
324+
"def string_is_int(a_str):\n",
325+
" try:\n",
326+
" int(a_str)\n",
327+
" return True\n",
328+
" except ValueError:\n",
329+
" return False\n",
330+
"\n",
331+
"an_int = '123'\n",
332+
"no_int = '123abc'\n",
333+
"\n",
334+
"%timeit string_is_int(an_int)\n",
335+
"%timeit string_is_int(no_int)\n",
336+
"%timeit an_int.isdigit()\n",
337+
"%timeit no_int.isdigit()\n",
338+
"\n",
339+
"#\n",
340+
"# Python 3.4.0\n",
341+
"# MacOS X 10.9.2\n",
342+
"# 2.5 GHz Intel Core i5\n",
343+
"# 4 GB 1600 Mhz DDR3\n",
344+
"#"
345+
],
346+
"language": "python",
347+
"metadata": {},
348+
"outputs": [
349+
{
350+
"output_type": "stream",
351+
"stream": "stdout",
352+
"text": [
353+
"1000000 loops, best of 3: 401 ns per loop\n",
354+
"100000 loops, best of 3: 3.04 \u00b5s per loop"
355+
]
356+
},
357+
{
358+
"output_type": "stream",
359+
"stream": "stdout",
360+
"text": [
361+
"\n",
362+
"10000000 loops, best of 3: 92.1 ns per loop"
363+
]
364+
},
365+
{
366+
"output_type": "stream",
367+
"stream": "stdout",
368+
"text": [
369+
"\n",
370+
"10000000 loops, best of 3: 96.3 ns per loop"
371+
]
372+
},
373+
{
374+
"output_type": "stream",
375+
"stream": "stdout",
376+
"text": [
377+
"\n"
378+
]
379+
}
380+
],
381+
"prompt_number": 5
382+
},
383+
{
384+
"cell_type": "markdown",
385+
"metadata": {},
386+
"source": [
387+
"<br>\n",
388+
"<br>\n",
389+
"<a name='is_number'></a>\n",
390+
"## Testing if a string is a number"
391+
]
392+
},
393+
{
394+
"cell_type": "code",
395+
"collapsed": false,
396+
"input": [
397+
"import timeit\n",
398+
"\n",
399+
"def string_is_number(a_str):\n",
400+
" try:\n",
401+
" float(a_str)\n",
402+
" return True\n",
403+
" except ValueError:\n",
404+
" return False\n",
405+
" \n",
406+
"a_float = '1.234'\n",
407+
"no_float = '123abc'\n",
408+
"\n",
409+
"a_float.replace('.','',1).isdigit()\n",
410+
"no_float.replace('.','',1).isdigit()\n",
411+
"\n",
412+
"%timeit string_is_number(an_int)\n",
413+
"%timeit string_is_number(no_int)\n",
414+
"%timeit a_float.replace('.','',1).isdigit()\n",
415+
"%timeit no_float.replace('.','',1).isdigit()\n",
416+
"\n",
417+
"#\n",
418+
"# Python 3.4.0\n",
419+
"# MacOS X 10.9.2\n",
420+
"# 2.5 GHz Intel Core i5\n",
421+
"# 4 GB 1600 Mhz DDR3\n",
422+
"#"
423+
],
424+
"language": "python",
425+
"metadata": {},
426+
"outputs": [
427+
{
428+
"output_type": "stream",
429+
"stream": "stdout",
430+
"text": [
431+
"1000000 loops, best of 3: 400 ns per loop\n",
432+
"1000000 loops, best of 3: 1.15 \u00b5s per loop"
433+
]
434+
},
435+
{
436+
"output_type": "stream",
437+
"stream": "stdout",
438+
"text": [
439+
"\n",
440+
"1000000 loops, best of 3: 452 ns per loop"
441+
]
442+
},
443+
{
444+
"output_type": "stream",
445+
"stream": "stdout",
446+
"text": [
447+
"\n",
448+
"1000000 loops, best of 3: 394 ns per loop"
449+
]
450+
},
451+
{
452+
"output_type": "stream",
453+
"stream": "stdout",
454+
"text": [
455+
"\n"
456+
]
457+
}
458+
],
459+
"prompt_number": 6
460+
},
461+
{
462+
"cell_type": "markdown",
463+
"metadata": {},
464+
"source": [
465+
"<br>\n",
466+
"<br>\n",
297467
"<a name='list_operations'></a>\n",
298468
"# List operations"
299469
]
@@ -620,6 +790,14 @@
620790
"### Conclusion\n",
621791
"Interestingly, the `try-except` loop pays off if we have more elements (here: 1000 integers instead of 100) as dictionary keys to check. Also, it doesn't matter much whether the elements exist or do not exist in the dictionary, yet."
622792
]
793+
},
794+
{
795+
"cell_type": "code",
796+
"collapsed": false,
797+
"input": [],
798+
"language": "python",
799+
"metadata": {},
800+
"outputs": []
623801
}
624802
],
625803
"metadata": {}

0 commit comments

Comments
 (0)