Created
May 21, 2024 07:29
-
-
Save 9seconds/daffc569593327d5495e89535da039a6 to your computer and use it in GitHub Desktop.
Concatenation
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 timeit | |
import string | |
SAMPLE = """ | |
[ | |
{ | |
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1", | |
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0", | |
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets", | |
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}", | |
"tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0", | |
"zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0", | |
"id": 1, | |
"node_id": "MDc6UmVsZWFzZTE=", | |
"tag_name": "v1.0.0", | |
"target_commitish": "master", | |
"name": "v1.0.0", | |
"body": "Description of the release", | |
"draft": false, | |
"prerelease": false, | |
"created_at": "2013-02-27T19:35:32Z", | |
"published_at": "2013-02-27T19:35:32Z", | |
"author": { | |
"login": "octocat", | |
"id": 1, | |
"node_id": "MDQ6VXNlcjE=", | |
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | |
"gravatar_id": "", | |
"url": "https://api.github.com/users/octocat", | |
"html_url": "https://github.com/octocat", | |
"followers_url": "https://api.github.com/users/octocat/followers", | |
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | |
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | |
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | |
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | |
"organizations_url": "https://api.github.com/users/octocat/orgs", | |
"repos_url": "https://api.github.com/users/octocat/repos", | |
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | |
"received_events_url": "https://api.github.com/users/octocat/received_events", | |
"type": "User", | |
"site_admin": false | |
}, | |
"assets": [ | |
{ | |
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1", | |
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip", | |
"id": 1, | |
"node_id": "MDEyOlJlbGVhc2VBc3NldDE=", | |
"name": "example.zip", | |
"label": "short description", | |
"state": "uploaded", | |
"content_type": "application/zip", | |
"size": 1024, | |
"download_count": 42, | |
"created_at": "2013-02-27T19:35:32Z", | |
"updated_at": "2013-02-27T19:35:32Z", | |
"uploader": { | |
"login": "octocat", | |
"id": 1, | |
"node_id": "MDQ6VXNlcjE=", | |
"avatar_url": "https://github.com/images/error/octocat_happy.gif", | |
"gravatar_id": "", | |
"url": "https://api.github.com/users/octocat", | |
"html_url": "https://github.com/octocat", | |
"followers_url": "https://api.github.com/users/octocat/followers", | |
"following_url": "https://api.github.com/users/octocat/following{/other_user}", | |
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", | |
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", | |
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions", | |
"organizations_url": "https://api.github.com/users/octocat/orgs", | |
"repos_url": "https://api.github.com/users/octocat/repos", | |
"events_url": "https://api.github.com/users/octocat/events{/privacy}", | |
"received_events_url": "https://api.github.com/users/octocat/received_events", | |
"type": "User", | |
"site_admin": false | |
} | |
} | |
] | |
} | |
] | |
""" | |
SAMPLE_CHUNKS = SAMPLE.split(",") | |
LETTERS_CHUNKS = list(string.printable) | |
def concat_with_plus(): | |
result = "" | |
for chunk in SAMPLE_CHUNKS: | |
result += chunk.upper() | |
return result | |
def concat_with_join_list_comprehension(): | |
# new list is intentional here, because usually we collect data in a | |
# new array | |
return "".join([el.upper() for el in SAMPLE_CHUNKS]) | |
def concat_with_join_loop(): | |
lst = [] | |
for chunk in SAMPLE_CHUNKS: | |
lst.append(chunk.upper()) | |
return "".join(lst) | |
def concat_with_plus_small(): | |
hostname = "github.com" | |
username = "9seconds" | |
return "https://" + hostname + "/" + username | |
def concat_with_join(): | |
hostname = "github.com" | |
username = "9seconds" | |
return "".join(["https://", hostname, "/", username]) | |
def concat_with_plus_letters(): | |
result = "" | |
for chunk in LETTERS_CHUNKS: | |
result += chunk.upper() | |
return result | |
def concat_with_join_letters(): | |
lst = [] | |
for chunk in LETTERS_CHUNKS: | |
lst.append(chunk.upper()) | |
return "".join(lst) | |
timers = { | |
func: timeit.Timer( | |
"main()", f"from __main__ import {func.__name__} as main" | |
) | |
for func in ( | |
concat_with_plus, | |
concat_with_join, | |
concat_with_join_loop, | |
concat_with_plus_small, | |
concat_with_join_letters, | |
concat_with_plus_letters, | |
) | |
} | |
for func, tme in timers.items(): | |
print(f"{func.__name__}: {tme.timeit()}") |
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
concat_with_plus: 8.804481650004163 | |
concat_with_join: 0.1536320090526715 | |
concat_with_join_loop: 5.991983758984134 | |
concat_with_plus_small: 0.16146991704590619 | |
concat_with_join_letters: 5.990820250008255 | |
concat_with_plus_letters: 6.699613924021833 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment