Synonyms/benchmark.py

89 lines
2.1 KiB
Python
Raw Normal View History

2017-10-21 11:45:15 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2017-10-31 16:54:55 +08:00
#=========================================================================
2017-10-21 11:45:15 +08:00
#
# Copyright (c) 2017 <> All Rights Reserved
#
#
# File: /Users/hain/ai/Synonyms/benchmark.py
# Author: Hai Liang Wang
# Date: 2017-10-21:11:26:53
#
2017-10-31 16:54:55 +08:00
#=========================================================================
2017-10-21 11:45:15 +08:00
"""
2017-10-31 16:54:55 +08:00
2017-10-21 11:45:15 +08:00
"""
from __future__ import print_function
from __future__ import division
__copyright__ = "Copyright (c) 2017 . All Rights Reserved"
2017-10-31 16:54:55 +08:00
__author__ = "Hai Liang Wang"
__date__ = "2017-10-21:11:26:53"
2017-10-21 11:45:15 +08:00
import os
import sys
import platform
import multiprocessing
curdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(curdir)
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding("utf-8")
# raise "Must be using Python 3"
import timeit
print("\nEnumerating Available System Resources...")
print("\n++++++++++ OS Name and version ++++++++++")
print("Platform:", platform.system())
print("Kernel:", platform.release())
print("Distro:", platform.linux_distribution())
print("Architecture:", platform.architecture())
print("\n++++++++++ CPU Cores ++++++++++")
p = os.popen("ps aux|awk 'NR > 0{s +=$3};END{print s}'").read()
print("Cores:", multiprocessing.cpu_count(), '\nCPU Load:', p)
print("\n++++++++++ System Memory ++++++++++\n")
2017-10-31 16:54:55 +08:00
2017-10-21 11:45:15 +08:00
def meminfo():
2017-10-31 16:54:55 +08:00
meminfo = dict()
2017-10-21 11:45:15 +08:00
with os.popen('cat /proc/meminfo') as f:
for line in f:
meminfo[line.split(':')[0]] = line.split(':')[1].strip()
return meminfo
2017-10-31 16:54:55 +08:00
2017-10-21 11:45:15 +08:00
try:
meminfo = meminfo()
print('Total Memory: {0}'.format(meminfo['MemTotal']))
print('Free Memory: {0}'.format(meminfo['MemFree']))
2017-10-31 16:54:55 +08:00
except BaseException:
2017-10-21 11:45:15 +08:00
print("meminfo unavailable")
2017-10-31 16:54:55 +08:00
2017-10-21 11:45:15 +08:00
def main():
repeat = 3
number = 100000
2017-10-31 16:54:55 +08:00
unit = "usec" # 微秒
2017-10-21 11:45:15 +08:00
unittosec = {"usec": 1e6, "msec": 1000, "sec": 1}
2017-10-31 16:54:55 +08:00
result = timeit.repeat(
"synonyms.nearby('人脸')",
"import synonyms",
number=number,
repeat=repeat)
2017-10-21 11:45:15 +08:00
print("%s: %d loops, best of %d epochs: %.3g %s per loop" %
2017-10-31 16:54:55 +08:00
("synonyms#nearby", number, repeat,
min(result) / number * unittosec[unit], unit))
2017-10-21 11:45:15 +08:00
if __name__ == '__main__':
main()