-
Notifications
You must be signed in to change notification settings - Fork 76
6 HW Verkh #564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VerkhVektor
wants to merge
3
commits into
DmitryChitalov:master
Choose a base branch
from
VerkhVektor:6
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
6 HW Verkh #564
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,72 @@ | |
| ВНИМАНИЕ: ЗАДАНИЯ, В КОТОРЫХ БУДУТ ГОЛЫЕ ЦИФРЫ ЗАМЕРОВ (БЕЗ АНАЛИТИКИ) | ||
| БУДУТ ПРИНИМАТЬСЯ С ОЦЕНКОЙ УДОВЛЕТВОРИТЕЛЬНО | ||
| """ | ||
| from random import randint | ||
| from memory_profiler import profile | ||
| from sys import getrefcount | ||
|
|
||
|
|
||
| @profile | ||
| def task_3_6(): | ||
| a = int(input('Введите количество элементов в массиве: ')) | ||
| b = [randint(0, 100) for el in range(a)] | ||
| print(b) | ||
| c = b.index(max(b)) | ||
| d = b.index(min(b)) | ||
| if abs(c - d) == 1: | ||
| print(f'Между максимальным элементом {max(b)} c индексом {c} и минимальным {min(b)} с индексом {d} нет чисел') | ||
| elif c < d: | ||
| print(f'Сумма элементов между минимальным {min(b)} и максимальным {max(b)} элементами: {sum(b[c:d])}') | ||
| elif c > d: | ||
| print(f'Сумма элементов между минимальным {min(b)} и максимальным {max(b)} элементами: {sum(b[d:c])}') | ||
| print(getrefcount(b)) | ||
| del b | ||
|
|
||
|
|
||
| task_3_6() | ||
|
|
||
| """Line # Mem usage Increment Line Contents | ||
| ================================================ | ||
| 20 16.2 MiB 16.2 MiB @profile | ||
| 21 def task_3_6(): | ||
| 22 16.2 MiB 0.0 MiB a = int(input('Введите количество элементов в массиве: ')) | ||
| 23 23.8 MiB 0.4 MiB b = [randint(0, 100) for el in range(a)] | ||
| 24 23.8 MiB 0.0 MiB print(b) | ||
| 25 23.8 MiB 0.0 MiB c = b.index(max(b)) | ||
| 26 23.8 MiB 0.0 MiB d = b.index(min(b)) | ||
| 27 23.8 MiB 0.0 MiB if abs(c - d) == 1: | ||
| 28 print(f'Между максимальным элементом {max(b)} c индексом {c} и | ||
| минимальным {min(b)} с индексом {d} нет чисел') | ||
| 29 23.8 MiB 0.0 MiB elif c < d: | ||
| 30 print(f'Сумма элементов между минимальным {min(b)} и | ||
| максимальным {max(b)} элементами: {sum(b[c:d])}') | ||
| 31 23.8 MiB 0.0 MiB elif c > d: | ||
| 32 23.8 MiB 0.0 MiB print(f'Сумма элементов между минимальным {min(b)} и | ||
| максимальным {max(b)} элементами: {sum(b[d:c])}') | ||
|
|
||
| По данным измерений видно, что большой инкремент памяти бы использован при генерации списка. Но для этого пришлось | ||
| использовать range = 10**6. Для оптимизации использования памяти нужно удалить значение b после использования кода | ||
| del b. | ||
|
|
||
| Line # Mem usage Increment Line Contents | ||
| ================================================ | ||
| 20 16.2 MiB 16.2 MiB @profile | ||
| 21 def task_3_6(): | ||
| 22 16.2 MiB 0.0 MiB a = int(input('Введите количество элементов в массиве: ')) | ||
| 23 24.7 MiB 0.6 MiB b = [randint(0, 100) for el in range(a)] | ||
| 24 24.7 MiB 0.0 MiB print(b) | ||
| 25 24.7 MiB 0.0 MiB c = b.index(max(b)) | ||
| 26 24.7 MiB 0.0 MiB d = b.index(min(b)) | ||
| 27 24.7 MiB 0.0 MiB if abs(c - d) == 1: | ||
| 28 print(f'Между максимальным элементом {max(b)} c индексом {c} и | ||
| минимальным {min(b)} с индексом {d} нет чисел') | ||
| 29 24.7 MiB 0.0 MiB elif c < d: | ||
| 30 print(f'Сумма элементов между минимальным {min(b)} и | ||
| максимальным {max(b)} элементами: {sum(b[c:d])}') | ||
| 31 24.7 MiB 0.0 MiB elif c > d: | ||
| 32 24.7 MiB 0.0 MiB print(f'Сумма элементов между минимальным {min(b)} и | ||
| максимальным {max(b)} элементами: {sum(b[d:c])}') | ||
| 33 17.1 MiB 0.0 MiB del b | ||
|
|
||
| после добавления del память вернулась к прежнему значению, не считая погрешности профайлера. | ||
| """ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. да |
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from random import randint | ||
| from memory_profiler import profile | ||
| from sys import getrefcount | ||
|
|
||
|
|
||
| @profile | ||
| def task_5(): | ||
| a = [randint(-100, 100) for i in range(100000)] | ||
| print(a) | ||
| b = [a[el] for el in range(len(a)) if a[el] < 0] | ||
| c = min(b, key=abs) | ||
| d = a.index(min(b, key=abs)) | ||
| print(f'Максимальный отрицательный элемент в данном массиве = {c}, его индекс {d}') | ||
| print(f'Количество ссылок на первый массив {getrefcount(a)}') | ||
| print(f'Количество ссылок на первый массив {getrefcount(b)}') | ||
|
|
||
|
|
||
| task_5() | ||
|
|
||
| """ | ||
| Line # Mem usage Increment Line Contents | ||
| ================================================ | ||
| 14 16.1 MiB 16.1 MiB @profile | ||
| 15 def task_5(): | ||
| 16 18.7 MiB 0.6 MiB a = [randint(-100, 100) for i in range(100000)] | ||
| 17 18.2 MiB 0.0 MiB print(a) | ||
| 18 18.6 MiB 0.1 MiB b = [a[el] for el in range(len(a)) if a[el] < 0] | ||
| 19 18.6 MiB 0.0 MiB c = min(b, key=abs) | ||
| 20 18.6 MiB 0.0 MiB d = a.index(min(b, key=abs)) | ||
| 21 18.6 MiB 0.0 MiB print(f'Максимальный отрицательный элемент в данном массиве = {c}, | ||
| его индекс {d}') | ||
|
|
||
| По данным замера так же видно, чтобольшую часть памяти берёт на себя генератор списка, при небольших значениях массивов | ||
| трата памяти незначительна, использовав range = 10**6 показало относительно большое использвание памяти в первом | ||
| массиве, и незначительное во втором (во многом благодаря генератору списков). | ||
| Для оптимизации кода слеудет удалить ссылки на массив a и b командой del после выполнения кода. | ||
|
|
||
| Не понятно, почему начальные 16.1 приросли до 18.7 при инкременте 0.6. Тоже погрешность? | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. мы сравниваем 16.1 приросли до 18.7 |
||
| """ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хорошо, вижу аналитику