Title: Micro-optimization: Use memmove instead of for loop in list.insert() · Issue #94508 · python/cpython · GitHub
Open Graph Title: Micro-optimization: Use memmove instead of for loop in list.insert() · Issue #94508 · python/cpython
X Title: Micro-optimization: Use memmove instead of for loop in list.insert() · Issue #94508 · python/cpython
Description: Background I noticed a difference in speed when inserting an element to the beginning of the list using list.insert vs list slice assignment. Upon inspecting the code, the usage of memmove in the list slice assignment code seems to be th...
Open Graph Description: Background I noticed a difference in speed when inserting an element to the beginning of the list using list.insert vs list slice assignment. Upon inspecting the code, the usage of memmove in the l...
X Description: Background I noticed a difference in speed when inserting an element to the beginning of the list using list.insert vs list slice assignment. Upon inspecting the code, the usage of memmove in the l...
Opengraph URL: https://github.com/python/cpython/issues/94508
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Micro-optimization: Use memmove instead of for loop in list.insert()","articleBody":"**Background**\r\n\r\nI noticed a difference in speed when inserting an element to the beginning of the list using list.insert vs list slice assignment. Upon inspecting the code, the usage of `memmove` in the list slice assignment code seems to be the providing a slight speed boost over the for loop used in the list.insert implementation.\r\n\r\nThe following code was used to benchmark the difference:\r\n\r\n```py\r\nimport random\r\nimport timeit\r\n\r\nruns = 100000\r\nsetup = 'import random;lst=[random.randrange(100000) for _ in range(100000)]; x = random.randrange(100000)'\r\n\r\nprint(timeit.timeit(stmt='lst[:0] = [x]', setup=setup, number=runs))\r\nprint(timeit.timeit(stmt='lst.insert(0, x)', setup=setup, number=runs))\r\n```\r\n\r\nOriginal:\r\n```\r\nSlice assignment: 7.394628149999335s\r\nlist.insert: 8.910868348000804s\r\n```\r\n\r\nUsing `memmove` in list.insert():\r\n```\r\nSlice assignment: 7.252985826999065s\r\nlist.insert: 7.215608489997976s\r\n```\r\n (Compiled with optimizations and tested on Arch Linux, kernel 5.18)","author":{"url":"https://github.com/radiantly","@type":"Person","name":"radiantly"},"datePublished":"2022-07-02T18:29:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":15},"url":"https://github.com/94508/cpython/issues/94508"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e73ae9c7-3800-ed63-cdd4-e499089bc893 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFAC:6AD60:1EF69DD:2B0B113:696937CF |
| html-safe-nonce | 448b393b6259bbde372ad75296ec57ff85035f7bb7cb17fa40ad7a2e132da223 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkFDOjZBRDYwOjFFRjY5REQ6MkIwQjExMzo2OTY5MzdDRiIsInZpc2l0b3JfaWQiOiIxNTE2MzQxMjI0OTgzMzEyMzM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 457a4eafa7c8b7505c448c773e36a0d1141bd9f0b7da5f303aba6536e2960498 |
| hovercard-subject-tag | issue:1292114389 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/94508/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ddbbe94f95cbd31972596d8d67ab0af4521876057911995585ce119c1ba6c2a7/python/cpython/issues/94508 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ddbbe94f95cbd31972596d8d67ab0af4521876057911995585ce119c1ba6c2a7/python/cpython/issues/94508 |
| og:image:alt | Background I noticed a difference in speed when inserting an element to the beginning of the list using list.insert vs list slice assignment. Upon inspecting the code, the usage of memmove in the l... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | radiantly |
| hostname | github.com |
| expected-hostname | github.com |
| None | 54182691a21263b584d2e600b758e081b0ff1d10ffc0d2eefa51cf754b43b51d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d69ac0477df0f87da03b8b06cebd187012d7a930 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width