-
-
Save pije76/0dc9879cecfdc85e34fc41e7a72d4025 to your computer and use it in GitHub Desktop.
list comprehension vs for loop python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pprint | |
phrase = """ Britney Jean Spears (born December 2, 1981) is an American singer, dancer and actress. Born in | |
McComb, Mississippi, and raised in Kentwood, Louisiana, she performed acting roles in stage | |
productions and television shows as a child before signing with Jive Records in 1997. Spears's first | |
and second studio albums, ...Baby One More Time (1999) and Oops!... I Did It Again (2000), became | |
international successes, with the former becoming the best-selling album by a teenage solo | |
artist.[1] Title tracks "...Baby One More Time" and "Oops!... I Did It Again" broke international | |
sales records. In 2001, Spears released her self-titled third studio album, Britney, and played the | |
starring role in the film Crossroads (2002). She assumed creative control of her fourth studio | |
album, In the Zone (2003), which yielded the worldwide success of the single "Toxic". | |
In 2007, Spears's much-publicized personal issues sent her career into hiatus. Her fifth studio | |
album, Blackout, was released later that year, and spawned singles such as "Gimme More" and "Piece | |
of Me". Her erratic behavior and hospitalizations continued through the following year, at which | |
point she was placed under a still ongoing conservatorship. Spears's sixth studio album, Circus | |
(2008), included the international chart-topping single "Womanizer". Her seventh studio album, Femme | |
Fatale (2011), became her first to yield three top-ten singles in the United States. She released | |
her eighth studio album Britney Jean in 2013. Later that year, Spears began the four-year residency | |
show, Britney: Piece of Me, at The AXIS at Planet Hollywood Resort & Casino in Las Vegas. In 2016, | |
Spears released her ninth studio album, Glory. | |
Spears is regarded as a pop icon and credited with influencing the revival of teen pop during the | |
late 1990s. She became the 'best-selling teenage artist of all time'[2] and garnered honorific | |
titles including the "Princess of Pop".[3] Her work has earned her numerous awards and accolades, | |
including a Grammy Award, six MTV Video Music Awards including the Lifetime Achievement Award, ten | |
Billboard Music Awards including the Billboard Millennium Award[4] and a star on the Hollywood Walk | |
of Fame. In 2009, Billboard ranked her as the 8th overall Artist of the Decade,[5] and also | |
recognized her as the best-selling female artist of the 2000s, as well as the fifth overall.[6] The | |
Recording Industry Association of America (RIAA) lists Spears as the eighth top-selling female | |
artist in the United States, with 34 million certified albums.[7] She has sold 100 million albums | |
and over 100 million singles worldwide[8][9][10] making her one of the best-selling music artists of | |
all time. According to Billboard Spears has sold about 22.38 million singles in digital downloads in | |
US and is the fourth best-selling female artist since Nielsen SoundScan began.[11] | |
Rolling Stone recognized her instant success as one of the Top 25 Teen Idol Breakout Moments of all | |
time,[12] while VH1 ranked her eleventh on their "100 Greatest Women in Music" list in 2012,[13] and | |
Billboard named her the sexiest woman in music.[14] Forbes reported that Spears was the highest paid | |
female musician of 2012, with earnings of $58 million, having last topped the list in 2002. """ | |
def naive(): | |
new_phrase = [] | |
for mot in phrase.split(' '): | |
if len(mot) < 4: | |
new_phrase.append(mot.lower()) | |
else: | |
new_phrase.append(mot.capitalize()) | |
return ' '.join(new_phrase) | |
def with_list_comprehension(): | |
return ' '.join( | |
[ | |
mot.lower() if len(mot) < 4 | |
else mot.capitalize() | |
for mot in phrase.split(' ') | |
] | |
) | |
def with_boolean(): | |
return " ".join( | |
[ | |
len(mot) < 4 and mot.lower() | |
or mot.capitalize() | |
for mot in phrase.split(' ') | |
] | |
) | |
def benchmark(*functions): | |
results = [] | |
result = None | |
for func in functions: | |
start = time.time() | |
for _ in range(10 ** 5): | |
formated = func() | |
if result is not None: | |
assert(result == formated) | |
else: | |
result = formated | |
results.append([func.__name__, time.time() - start]) | |
return sorted(results, key=lambda func: func[1]) | |
pp = pprint.PrettyPrinter(indent=4) | |
benchs = benchmark(naive, with_list_comprehension, with_boolean) | |
for bench in benchs: | |
bench.append('{}% slower'.format(int((bench[1] - benchs[0][1]) * 100 / benchs[0][1]))) | |
print(benchs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment