Skip to content
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

Improve execution speed #5941

Closed
wants to merge 1 commit into from

Conversation

changxuqing
Copy link
Contributor

The operations of converting characters to ASCII values and generating a list of bits can be combined into one loop, reducing the number of passes.

@changxuqing changxuqing force-pushed the master branch 2 times, most recently from e8f662c to 9425bd7 Compare May 29, 2024 03:14
@changxuqing changxuqing changed the title Merge two loops Improve execution speed May 29, 2024
@changxuqing
Copy link
Contributor Author

changxuqing commented May 29, 2024

def string_to_bitlist(data):
ord_ = ord
result = []
append = result.append
for ch in data:
ascii_value = ord_(ch)
for i in range(7, -1, -1):
append((ascii_value >> i) & 1)
return result

def string_to_bitlist2(data):
data = [ord(c) for c in data]
result = []
for ch in data:
i = 7
while i >= 0:
if ch & (1 << i) != 0:
result.append(1)
else:
result.append(0)
i -= 1
return result

import time

def test_method1(data):
start_time = time.time()
result = string_to_bitlist(data)
end_time = time.time()
print("Method 1 Execution Time:", end_time - start_time)
return result

def test_method2(data):
start_time = time.time()
result = string_to_bitlist2(data)
end_time = time.time()
print("Method 2 Execution Time:", end_time - start_time)
return result

import timeit

test_string = "Hello, World!" *1000
number_of_executions = 1000
time1 = timeit.timeit('string_to_bitlist(test_string)', globals=globals(), number=number_of_executions)
print(f"string_to_bitlist: {time1} seconds")
time2 = timeit.timeit('string_to_bitlist2(test_string)', globals=globals(), number=number_of_executions)
print(f"string_to_bitlist2: {time2} seconds")

Hi ,@richtja I compared the speed of the two methods, and the results show that the speed is faster than before when dealing with large data.Here are the results

string_to_bitlist: 8.632775765028782 seconds
string_to_bitlist2: 10.137682066997513 seconds

@richtja richtja self-requested a review May 29, 2024 13:26
Copy link
Contributor

@richtja richtja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @changxuqing, thank you for your changes. It LGTM. Can you please do a rebase to master? It should solve the CI issues.

Each step of character processing is directly reflected in a
continuous loop with no intermediate state, which makes the
code easier to understand and maintain.

Signed-off-by: changxuqing <changxuqing@uniontech.com>
@richtja
Copy link
Contributor

richtja commented Jun 19, 2024

Hi @changxuqing, is there any reason for closing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants