Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Created July 9, 2020 18:02
Show Gist options
  • Save mikeckennedy/00828db1d49d2cd2dac8fa0295e54c23 to your computer and use it in GitHub Desktop.
Save mikeckennedy/00828db1d49d2cd2dac8fa0295e54c23 to your computer and use it in GitHub Desktop.
Simple example to test how much faster or slower it is to simply try and convert then catch an error vs. testing up front.
import datetime
import random
from typing import List
def main():
random.seed(172)
count = 1_000_000
data = build_data(count)
run_with_except(data)
run_with_test(data)
def build_data(count: int):
letters = "abcdefghijklmnopqrstuv"
numbers = list(range(1, 11))
combined = list(letters) + numbers
return [
str(random.choice(combined))
for _ in range(count)
]
def run_with_except(data: List):
t0 = datetime.datetime.now()
numbers = 0
for item in data:
try:
n = int(item)
numbers += 1
except:
pass
dt = datetime.datetime.now() - t0
print(
f'EXCEPT: Done in {dt.total_seconds() * 1000:,.0f} ms, {numbers:,} numbers and {len(data) - numbers:,} letters.')
def run_with_test(data: List):
t0 = datetime.datetime.now()
numbers = 0
for item in data:
if item.isnumeric():
n = int(item)
numbers += 1
dt = datetime.datetime.now() - t0
print(
f'TEST: Done in {dt.total_seconds() * 1000:,.0f} ms, {numbers:,} numbers and {len(data) - numbers:,} letters.')
if __name__ == '__main__':
main()
@mikeckennedy
Copy link
Author

mikeckennedy commented Nov 4, 2021

@madig Other languages (like C#) have non-exception based versions in addition to this. For example:

// similar style
int x = int.Parse(text)

// exception free (but clumsier) style
int x;
if (!int.TryParse(text, out x))
{
    // error, not an int
}

@theArtechnology Perhaps :) But major changes to the syntax are probably not worth it until this because one of the slower parts of Python. There are many more pieces of low hanging fruit before worrying about this TOO much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment