You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DataCamp_Notes/Writing Efficient Python Code.txt
+141Lines changed: 141 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -377,6 +377,147 @@ Timing and profiling code
377
377
In this chapter, you will learn how to gather and compare runtimes between different coding approaches. You'll practice using the line_profiler and memroy_profiler packages to profile your code base and spot bottlenecks. Then, you'll put your learnings to practice by replacing these bottlenecks with efficient Python code.
You'd like to create a list of integers from 0 to 50 using the range() function. However, you are unsure whether using list comprehension or unpacking the range object into a list is faster. Let's use %timeit to find the best implementation.
384
+
385
+
For your convenience, a reference table of time orders of magnitude is provided below (faster at the top).
386
+
387
+
symbol name unit (s)
388
+
ns nanosecond 10-9
389
+
μs (us) microsecond 10-6
390
+
ms millisecond 10-3
391
+
s second 100
392
+
Instructions 1/3
393
+
35 XP
394
+
1
395
+
2
396
+
3
397
+
Use list comprehension and range() to create a list of integers from 0 to 50 called nums_list_comp.
398
+
399
+
# Create a list of integers (0-50) using list comprehension
400
+
nums_list_comp = [num for num in range(0,51)]
401
+
print(nums_list_comp)
402
+
403
+
404
+
405
+
Use range() to create a list of integers from 0 to 50 and unpack its contents into a list called nums_unpack.
406
+
407
+
# Create a list of integers (0-50) using list comprehension
408
+
nums_list_comp = [num for num in range(51)]
409
+
print(nums_list_comp)
410
+
411
+
# Create a list of integers (0-50) by unpacking range
412
+
nums_unpack = [*(nums_list_comp)]
413
+
print(nums_unpack)
414
+
415
+
416
+
Question
417
+
Use %timeit within your IPython console to compare the runtimes for creating a list of integers from 0 to 50 using list comprehension vs. unpacking the range object. Don't include the print() statements when timing.
Using %timeit: specifying number of runs and loops
424
+
A list of 480 superheroes has been loaded into your session (called heroes). You'd like to analyze the runtime for converting this heroes list into a set. Instead of relying on the default settings for %timeit, you'd like to only use 5 runs and 25 loops per each run.
425
+
426
+
What is the correct syntax when using %timeit and only using 5 runs with 25 loops per each run?
427
+
428
+
Possible Answers
429
+
timeit -runs5 -loops25 set(heroes)
430
+
%%timeit -r5 -n25 set(heroes)
431
+
%timeit set(heroes), 5, 25
432
+
%timeit -r5 -n25 set(heroes)
433
+
434
+
+50 XP
435
+
Correct! %timeit lets you specify the number of runs and number of loops you want to consider with the -r and -n flags. You can use -r5 and -n25 to specify 5 iterations each with 25 loops when calculating the average and standard deviation of runtime for your code.
Python allows you to create data structures using either a formal name or a literal syntax. In this exercise, you'll explore how using a literal syntax for creating a data structure can speed up runtimes.
441
+
442
+
data structure formal name literal syntax
443
+
list list() []
444
+
dictionary dict() {}
445
+
tuple tuple() ()
446
+
Instructions 1/3
447
+
35 XP
448
+
1
449
+
2
450
+
3
451
+
Create an empty list called formal_list using the formal name (list()).
452
+
Create an empty list called literal_list using the literal syntax ([]).
453
+
454
+
455
+
# Create a list using the formal name
456
+
formal_list = list()
457
+
print(formal_list)
458
+
459
+
# Create a list using the literal syntax
460
+
literal_list = []
461
+
print(literal_list)
462
+
463
+
464
+
465
+
Print out the type of formal_list and literal_list to show that both naming conventions create a list.
466
+
467
+
# Create a list using the formal name
468
+
formal_list = list()
469
+
print(formal_list)
470
+
471
+
# Create a list using the literal syntax
472
+
literal_list = []
473
+
print(literal_list)
474
+
475
+
# Print out the type of formal_list
476
+
print(type(formal_list))
477
+
478
+
# Print out the type of literal_list
479
+
print(type(literal_list))
480
+
481
+
482
+
Instructions 3/3
483
+
30 XP
484
+
3
485
+
Question
486
+
Use %timeit in your IPython console to compare runtimes between creating a list using the formal name (list()) and the literal syntax ([]). Don't include the print() statements when timing.
487
+
488
+
Which naming convention is faster?
489
+
490
+
+100 XP
491
+
Great job! Using Python's literal syntax to define a data structure can speed up your runtime. Consider using the literal syntaxes (like [] instead of list(), {} instead of dict(), or () instead of tuple()), where applicable, to gain some speed.
From here on out, you'll be working with a superheroes dataset. For this exercise, a list of each hero's weight in kilograms (called wts) is loaded into your session. You'd like to convert these weights into pounds.
499
+
500
+
You could accomplish this using the below for loop:
501
+
502
+
hero_wts_lbs = []
503
+
for wt in wts:
504
+
hero_wts_lbs.append(wt * 2.20462)
505
+
Or you could use a numpy array to accomplish this task:
506
+
507
+
wts_np = np.array(wts)
508
+
hero_wts_lbs_np = wts_np * 2.20462
509
+
Use %%timeit in your IPython console to compare runtimes between these to approaches. Make sure to press SHIFT+ENTER after the magic command to add a new line before writing the code you wish to time. After you've finished coding, answer the following question:
510
+
511
+
Which of the above techniques is faster?
512
+
513
+
514
+
+50 XP
515
+
Nice work! You used %%timeit (cell magic mode) to time multiple lines of code. Converting the wts list into a NumPy array and taking advantage of NumPy array broadcasting saved you some time! Moving forward, remember that you can use %timeit to gather runtime for a single line of code (line magic mode) and %%timeit to get the runtime for multiple lines of code.
0 commit comments