Skip to content

Instantly share code, notes, and snippets.

@nekoya
Created March 5, 2013 07:22
Show Gist options
  • Save nekoya/5088603 to your computer and use it in GitHub Desktop.
Save nekoya/5088603 to your computer and use it in GitHub Desktop.
Python benchmark (mcrypt, Crypto)
# -*- coding: utf-8 -*-
from benchmarker import Benchmarker
import base64
import mcrypt
from Crypto.Cipher import Blowfish
key = 'thisiskey'
iv = '123abc45'
plain_text = 'hello world\0\0\0\0\0'
encrypted = 'VFkA5lnNeEc0R8HeA5/7ng=='
mcipher = mcrypt.MCRYPT('blowfish', 'cbc')
mcipher.init(key, iv)
def mcrypt_enc():
cipher = mcrypt.MCRYPT('blowfish', 'cbc')
cipher.init(key, iv)
assert base64.b64encode(cipher.encrypt(plain_text)) == encrypted
def mcrypt_dec():
cipher = mcrypt.MCRYPT('blowfish', 'cbc')
cipher.init(key, iv)
assert cipher.decrypt(base64.b64decode(encrypted)) == plain_text
def mcrypt_enc_reinit():
mcipher.reinit()
assert base64.b64encode(mcipher.encrypt(plain_text)) == encrypted
def mcrypt_dec_reinit():
mcipher.reinit()
assert mcipher.decrypt(base64.b64decode(encrypted)) == plain_text
def crypto_enc():
cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
assert base64.b64encode(cipher.encrypt(plain_text)) == encrypted
def crypto_dec():
cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
assert cipher.decrypt(base64.b64decode(encrypted)) == plain_text
width = 40
loop = 3000
def bench_gmtime():
with Benchmarker(width=width, loop=loop) as bm:
for _ in bm('mcrypt_enc'):
mcrypt_enc()
for _ in bm('mcrypt_dec'):
mcrypt_dec()
for _ in bm('mcrypt_enc_reinit'):
mcrypt_enc_reinit()
for _ in bm('mcrypt_dec_reinit'):
mcrypt_dec_reinit()
for _ in bm('crypto_enc'):
crypto_enc()
for _ in bm('crypto_dec'):
crypto_dec()
if __name__ == '__main__':
bench_gmtime()
@nekoya
Copy link
Author

nekoya commented Mar 5, 2013

## benchmarker:       release 3.0.1 (for python)
## python platform:   linux2 [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
## python version:    2.6.5
## python executable: /kauli/python/env1/bin/python

##                                           user       sys     total      real
mcrypt_enc                                 1.8400    0.0400    1.8800    1.8980
mcrypt_dec                                 1.7100    0.0100    1.7200    1.7418
mcrypt_enc_reinit                          0.0000    0.0000    0.0000    0.0052
mcrypt_dec_reinit                          0.0100    0.0000    0.0100    0.0052
crypto_enc                                 0.1200    0.0000    0.1200    0.1189
crypto_dec                                 0.1200    0.0000    0.1200    0.1192

## Ranking                                   real
mcrypt_dec_reinit                          0.0052 (100.0%) *************************
mcrypt_enc_reinit                          0.0052 ( 98.4%) *************************
crypto_enc                                 0.1189 (  4.3%) *
crypto_dec                                 0.1192 (  4.3%) *
mcrypt_dec                                 1.7418 (  0.3%) 
mcrypt_enc                                 1.8980 (  0.3%) 

## Ratio Matrix                              real     [01]     [02]     [03]     [04]     [05]     [06]
[01] mcrypt_dec_reinit                     0.0052   100.0%   101.6%  2306.9%  2311.5% 33782.5% 36811.8%
[02] mcrypt_enc_reinit                     0.0052    98.4%   100.0%  2270.8%  2275.3% 33253.5% 36235.4%
[03] crypto_enc                            0.1189     4.3%     4.4%   100.0%   100.2%  1464.4%  1595.7%
[04] crypto_dec                            0.1192     4.3%     4.4%    99.8%   100.0%  1461.5%  1592.5%
[05] mcrypt_dec                            1.7418     0.3%     0.3%     6.8%     6.8%   100.0%   109.0%
[06] mcrypt_enc                            1.8980     0.3%     0.3%     6.3%     6.3%    91.8%   100.0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment