Skip to content

Conversation

@BonePolk
Copy link
Contributor

@BonePolk BonePolk commented Nov 1, 2025

I replaces the custom pure-Python KMPSearch implementation with the built-in bytes.find() method, and not it works much faster about 100x!!!

Builtin function is written in C and use more effective algorithm for byte pattern searching.

Testing with

import time
from PyMemoryEditor.util.search.kmp import KMPSearch


def generate_text(size):
    # Return a random text.
    return "".join([chr(random.randint(ord("A"), ord("Z"))) for letter in range(size)])


def test_search_speed():
    target_value_size = 8
    target_value = generate_text(target_value_size).encode()
    memory_region_data_size = target_value_size*1024*1024
    memory_region_data = generate_text(memory_region_data_size).encode()

    start_time = time.perf_counter()
    found_index = memory_region_data.find(target_value, 0)
    while found_index != -1:
        found_index = memory_region_data.find(target_value, found_index+1)
    builtin_time  = time.perf_counter() - start_time
    print(f"builtin find() time: {builtin_time:.6f} sec")

    start_time = time.perf_counter()
    searcher = KMPSearch(target_value, target_value_size)
    for found_index in searcher.search(memory_region_data, memory_region_data_size):
        pass
    kmp_time = time.perf_counter() - start_time
    print(f"KMP search time: {kmp_time:.6f} sec")
    print(f"{kmp_time / builtin_time:.1f}x faster")
    
    
test_search_speed()

Show results:

builtin find() time: 0.014394 sec
KMP search time: 1.522867 sec
105.8x faster

Some OS Information

OS Information: Windows-10-10.0.26100-SP0 - WindowsPE 64bit
Processor Information: AMD64 | Intel64 Family 6 Model 154 Stepping 4, GenuineIntel

@JeanExtreme002
Copy link
Owner

Hi, @BonePolk !

Sorry for the delay in getting back to this. I’ve reviewed the changes and they do indeed improve the performance of the search. Everything looks good, so I’ll be merging this PR. Thanks for the improvement! 🙌

@JeanExtreme002 JeanExtreme002 merged commit 979d18e into JeanExtreme002:master Dec 28, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants