Skip to content

Instantly share code, notes, and snippets.

@mylons
Created September 14, 2015 18:10
Show Gist options
  • Save mylons/fd852d2b7246f75cd7f2 to your computer and use it in GitHub Desktop.
Save mylons/fd852d2b7246f75cd7f2 to your computer and use it in GitHub Desktop.
run time interview questions
# assume inputs are lists
def merge1(words, more):
sentence = ""
for w in words: sentence += w
for w in more: sentence += w
return sentence
def merge2(words, more):
sentence = []
for w in words: sentence.append(w)
for w in more: sentence.append(w)
return sentence
@jmanning2k
Copy link

name rank runs mean sd timesBaseline
merge1 1 100 0.0005998 4.408e-05 1.0
merge2 2 100 0.0006683 5.168e-05 1.1142120075

So, merge1 is actually faster contrary to our assumptions.

@mylons
Copy link
Author

mylons commented Sep 16, 2015

haha, something about concatenating strings in python? maybe a string is actually just a list in python and you're getting the auto-expanding behavior?

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