Both these functions take in a sentence and generate a random string based of the length of sentence
.
Mine, the first one, runs 4 times more slowly than the second one (someone else's code), but I don't know why and I'd like to understand what's happening 'under the hood' that makes it this way. If anything I would've expected numpy to be faster, which makes me think it's something else entirely.
Also, is there a way to make the first function even faster?
import random
import numpy as np
def generate_guess(sentence):
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']
return [np.random.choice(alphabet) for i in range(len(sentence))]
Don't review this one:
def generate_guess(sentence): alphabet = 'abcdefghijklmnopqrstuvwxyz ' res = '' for i in range(len(sentence)): res = res + alphabet[random.randrange(len(alphabet))] return res