<commit_before>#!/usr/bin/python

# Simple benchmark for Capstone by disassembling random code. By <NAME>, 2014

from capstone import *

from time import time
from random import randint


def random_str(size):
    lst = [str(randint(0, 255)) for _ in xrange(size)]
    return "".join(lst)

def cs(md, data):
    insns = md.disasm(data, 0)
    # uncomment below line to speed up this function 200 times!
    # return
    for i in insns:
        if i.address == 0x100000:
            print i

md = Cs(CS_ARCH_X86, CS_MODE_32)
md.detail = False

# warm up few times
for i in xrange(3):
    data = random_str(128)
    cs(md, data)

# start real benchmark
c_t = 0
for i in xrange(10000):
    code = random_str(128)

    t1 = time()
    cs(md, code)
    c_t += time() - t1


print "Capstone:", c_t, "seconds"<commit_msg>suite: benchmark.py get disasm code from binary file (python itself) rather than randomize data - this stablizes results, and can be compared with other bindings<commit_after>#!/usr/bin/python

# Simple benchmark for Capstone by disassembling random code. By <NAME>, 2014

from capstone import *

from time import time
from random import randint


# file providing code to disassemble
FILE = '/usr/bin/python'


def get_code(f, size):
    code = f.read(size)
    if len(code) != size:  # reached end-of-file?
        f.seek(0)
        code = f.read(size)

    return code


def cs(md, code):
    insns = md.disasm(code, 0)
    # uncomment below line to speed up this function 200 times!
    # return
    for i in insns:
        if i.address == 0x100000:
            print i


md = Cs(CS_ARCH_X86, CS_MODE_32)
md.detail = False

cfile = open(FILE)

# warm up few times
for i in xrange(3):
    code = get_code(cfile, 128)
    cs(md, code)

# start real benchmark
c_t = 0
for i in xrange(10000):
    code = get_code(cfile, 128)

    t1 = time()
    cs(md, code)
    c_t += time() - t1


print "Capstone:", c_t, "seconds"
{'fraction_non_alphanumeric': 0.10668047643707923, 'fraction_numerical': 0.03780424650440187, 'mean_word_length': 2.942857142857143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7443105', 'n_tokens_mistral': 746, 'n_tokens_neox': 669, 'n_words': 234}