Skip to content

Commit

Permalink
python: implement disasm_lite() method which only return tuples of so…
Browse files Browse the repository at this point in the history
…me critical info. this improves performance by 15%
  • Loading branch information
aquynh committed Feb 20, 2014
1 parent 6bc08a8 commit d53c165
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
15 changes: 15 additions & 0 deletions bindings/python/capstone/capstone.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,18 @@ def disasm(self, code, offset, count = 0):
return
yield

def disasm_lite(self, code, offset, count = 0):
all_insn = ctypes.POINTER(_cs_insn)()
res = _cs.cs_disasm_ex(self.csh, code, len(code), offset, count, ctypes.byref(all_insn))
if res > 0:
for i in xrange(res):
insn = all_insn[i]
yield (insn.address, insn.size, insn.mnemonic, insn.op_str)
_cs.cs_free(all_insn, res)
else:
status = _cs.cs_errno(self.csh)
if status != CS_ERR_OK:
raise CsError(status)
return
yield

25 changes: 24 additions & 1 deletion suite/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def cs(md, code):
print i


def cs_lite(md, code):
insns = md.disasm_lite(code, 0)
# uncomment below line to speed up this function 200 times!
# return
for (addr, size, mnem, ops) in insns:
if addr == 0x100000:
print i


cfile = open(FILE)

for (arch, mode, comment, syntax) in all_tests:
Expand Down Expand Up @@ -97,7 +106,21 @@ def cs(md, code):
cs(md, code)
c_t += time() - t1

print "Benchmark:", c_t, "seconds"
print "Benchmark - full obj:", c_t, "seconds"
print

cfile.seek(0)
c_t = 0
for i in xrange(50000):
code = get_code(cfile, 128)
#print to_hex(code)
#print

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

print "Benchmark - lite:", c_t, "seconds"
print
except CsError as e:
print("ERROR: %s" %e)

0 comments on commit d53c165

Please sign in to comment.