Learn Python in Arabic #145 - Numpy - Compare Performance And Memory Use

preview_player
Показать описание
Numpy - Compare Performance And Memory Use

=============================

Support Me on Patreon to Help me Create More Videos

I will be Very Happy if you Support my Channel.

Join Our Facebook Group:

Follow My Facebook Profile:

Like Facebook Page:

Follow Me On Twitter:
Рекомендации по теме
Комментарии
Автор

ربنا يجازيك كل خير ويجعله في ميزان حسناتك

mahmoudmegahed
Автор

thank You Teacher, Great Level Course .
Small Advice For All Programmers And For You Teacher,
Using time.perf_counter() To Count Execution Time Is More Accurate Than Subtract The Different Between Start-Time And End-Time Using time.time() .
Good Luck For All, And Thank You Again

abodawead
Автор

Return Value and Parameters of np.arange()
• NumPy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray class (number of dimensions array) with evenly spaced values and returns the reference to it.
• You can define the interval of the values contained in an array, space between them, and their type with four parameters of arange():
○ numpy.arange([start, ]stop, [step, ], dtype=None) -> numpy.ndarray
• The first three parameters determine the range of the values, while the fourth specifies the type of the elements:
○ start is the number (integer or decimal) that defines the first value in the array.
○ stop is the number that defines the end of the array and isn’t included in the array.
○ step is the number that defines the spacing (difference) between each two consecutive values in the array and defaults to 1.
○ dtype is the type of the elements of the output array and defaults to None.
• step can’t be zero. Otherwise, you’ll get a ZeroDivisionError. You can’t move away anywhere from start if the increment or decrement is 0.
• You can find more information on the parameters and the return value of arange() in the official documentation.

Example about np.arange()
import numpy as np
# Counting Forwards
array_one = np.arange(start=10, stop=100, step=10, dtype=None)
print(array_one) # [10 20 30 40 50 60 70 80 90]
# Counting Backwards
array_two = np.arange(start=10, stop=0, step=-1)
print(array_two) # [10 9 8 7 6 5 4 3 2 1]

numpy.ndarray.itemsize()
• function return the length of one array element in bytes.
Numpy size()
• In Python, numpy.size() function count the number of elements along a given axis.

Note:
• This is a short way to change and push values into a list
[(n1 + n2) for n1, n2 in zip(my_list1, my_list2)]
• The difference between making the conjunction for two lists and two arrays
my_list1 = range(elements)
my_list2 = range(elements)

my_array1 = np.arange(elements)
my_array2 = np.arange(elements)

list_result = [(n1 + n2) for n1, n2 in zip(my_list1, my_list2)]
array_result = my_array1 + my_array2

sys.getsizeof
• The getsizeof() is a system-specific method and hence we have to import the sys module to use it. A sample code is as shown below for calculating the size of a list.
• For example, the getsizeof() method returns 64 bytes for an empty list and then 8 bytes for every additional element.

Comparing the time consumer between lists and arrays
import numpy as np
import time

numberOfItems = 87984659
list_one = range(numberOfItems)
list_two = range(numberOfItems)

array_one = np.arange(numberOfItems)
array_two = np.arange(numberOfItems)

timeStartList = time.time()
lists = [(x + y) for x, y in zip(list_one, list_two)]
timeEndList = time.time()
timeStartArray = time.time()
arrays = array_one + array_two
timeEndArray = time.time()

print(timeEndList - timeStartList) # 11.686883449554443
print(timeEndArray - timeStartArray) # 0.13801336288452148

Comparing the memory use for arrays and lists
import numpy as np
import sys

numberOfItems = 100
list_one = range(numberOfItems)
array_one = np.arange(numberOfItems)
lists = [(x) for x in list_one]

print(array_one.itemsize)
print(array_one.size)


print(len(lists))

print(f"The size for List: {len(lists) * sys.getsizeof(lists[1])}")
# The size for List: 2800
print(f"The size for Array: {array_one.size * array_one.itemsize}")
# The size for Array: 400

Python
#
# -- Numpy => Compare Performance And Memory Use --
#
# - Performance
# - Memory Use
#

import numpy as np
import time
import sys

elements = 150000

my_list1 = range(elements)
my_list2 = range(elements)

my_array1 = np.arange(elements)
my_array2 = np.arange(elements)

list_start = time.time()
list_result = [(n1 + n2) for n1, n2 in zip(my_list1, my_list2)]
print(f"List Time: {time.time() - list_start}")

array_start = time.time()
array_result = my_array1 + my_array2
print(f"Array Time: {time.time() - array_start}")

# for n1, n2 in zip(my_list1, my_list2):
# print(n1 + n2)

print(list_result)
print(array_result)

my_array = np.arange(100)

print(my_array)
print(my_array.itemsize)
print(my_array.size)
print(f"All Bytes: {my_array.itemsize * my_array.size}")

print("#" * 50)

my_list = range(100)
print(sys.getsizeof(1))
print(len(my_list))
print(f"All Bytes: {sys.getsizeof(1) * len(my_list)}")

wdjamilmh
Автор

Char : 1byte
Int : 4 bytes
Float: 8 bytes

INGOAL
Автор

الحمد لله الواد اللي بيعمل ديس لايك لسة مدخلش على الفيديو ده ههههههههههههه

elarapy
Автор

ياترى ميزات الـ آراي في البايثون من حيث المساحة وسرعة الأداء موجودة نفسها في اللغات الثانية, جافا أو سي بلس بلس ؟

jamalmonawer
Автор

في مشكلة هنا وياريت لو يبقي في توضيح من البشمهندس أسامه ، في performance test
في حالة list حضرتك عملت list elements بينما في حالة array لم نقم بعملية list و بالتالي في اختلاف كبير جدا في ال performance test
ارجو التوضيح و شكرا جزيلا

mohamedfathi-zryg
Автор

يا باشمهندس لو سمحت ليه في المقارنه بين اللفت والري بنحط اللثه على انه متغير عادي نشكرك اقواس مربعه هل هي نفسها ولا هتفرق

youssefsamir
Автор

Type(my_list) => range()
مش list .

bachirnass
Автор

i, مفيش شهادة على الكورس التعلمي ده
Is There A Certificate for That Course

codingkids
Автор

I Got List Time: 2.5...
& Array Time: 4.2...
Any explanation pls! 🤔

oussamahamdi
Автор

لو سمحت هى السلسلة ديه باقيلها كام فيديو

mohamedmagdy
Автор

ليه مفيش تجديد لكورس SQL وفقا لاحتياجها فى Data Analysis

SamehRSameh
Автор

حطيت في الالمنتس و الجهاز معادش يستجيب ههه😂

kourdroid