celery笔记 jieba RPC 服务 http2 vs http1 python发送邮件 gitbook 笔记 docker运行 pyppeteer 百度/腾讯 ocr 试用 页面元素选择 python pickle 实践 k3s 安装加速 FFmpeg 使用总结 Systemd 教程 mysql 1366 错误解决 docker-compose 笔记 sqlite 使用总结 百度网盘命令行工具 bypy 阿里云 PAI-EAS 试用报告 gpt2中文预训练模型试用 文本生成资料汇总 使用 tracemalloc 分析 python 内存使用情况 spark 集群试用 openresty使用笔记 mac下 python 报错 CERTIFICATE_VERIFY_FAILED docker-compose 安装方法 系统代理 mac 下安装 adb scrapy项目作为工具库使用 charles over proxy 使用 markdown 制作 ppt docker挂载目录异常 flask 笔记 wsl2 使用体验 nginx 配置 mac 配置 发布自己的 python 包 selenium + chrome 全页面截图 mongo ORM 笔记 supervisor 使用总结 h5py性能测评 privoxy实现PAC代理上网 session请求示例 ssh笔记 python小技巧 docker学习笔记 tornado使用总结 再读《MongoDB权威指南》 tornado文件上传服务 mongo学习笔记 python异步服务器测试 No module named 'Crypto' on Mac mac中安装python3.5 py3.6环境下numpy C扩展出错 mtcnn读书笔记 shell 学习笔记 install ubuntu18.04 定时备份linux系统的history记录 asyncio异步请求示例 golang setting git使用笔记 Ubuntu16.04下配置python3环境 将Ubuntu16.04升级为Ubuntu18.04(development branch) Ubuntu16.04下源码安装python3.6 virtualenv中安装anaconda模块 基于sqlite3实现数据缓存 修复colaboratory中tensorflow的bug 安装docker-compose docker引起的空间不足 CNN可视化研究 ubuntu16.04中安装wine-qq 在ubuntu16.04中安装wine3.0+winetricks ssh over socks5 python删除文件或目录 shadowsocks+privoxy设置本地代理 python下载大文件的方法 解决python中遇到的乱码问题 修改 ubuntu & windows双系统中系统启动顺序与等待时间 python3安装mysql ubuntu环境变量设置 python 后台程序实现

h5py性能测评

2019年05月04日

h5py性能测评

代码

import pickle
import sys
import time
import unittest

import h5py
import numpy as np
import os


class TestH5(unittest.TestCase):
    def setUp(self):
        self.pickle_file = "./data.pkl"
        self.h5_file = "./data.h5"

    def tearDown(self):
        os.remove(self.pickle_file)
        os.remove(self.h5_file)

    @staticmethod
    def get_file_size(file_path):
        file_size = os.path.getsize(file_path) / float(1024 * 1024)
        return "{}MB".format(round(file_size, 2))

    @staticmethod
    def get_size(obj):
        return sys.getsizeof(obj)

    def create_file(self):
        """ 创建文件 """
        data = np.random.random(size=(100000, 1024))
        print("size of data is {}".format(self.get_size(data)))
        target_index = [1, 5, 10, 50, 100, 500, 1000, 5000, 9000, 9001, 9003]
        target_result = data[target_index]
        print("size of target_result is {}".format(self.get_size(target_result)))

        # pickle
        with open(self.pickle_file, "wb") as fw:
            pickle.dump(data, fw)
        print("pickle file size is {}".format(self.get_file_size(self.pickle_file)))

        # h5py
        with h5py.File(self.h5_file, 'w') as hf:
            hf.create_dataset('data', data=data)
        print("h5 file size is {}".format(self.get_file_size(self.h5_file)))

        return target_index, target_result

    def pickle_load(self, target_index, target_result):
        time_start = time.time()
        with open(self.pickle_file, "rb") as fr:
            all_data = pickle.load(fr)
            self.assertTrue((target_result == all_data[target_index]).all())
        return time.time() - time_start

    def h5py_load(self, target_index, target_result):
        time_start = time.time()
        with h5py.File(self.h5_file, 'r') as hf:
            all_data = hf["data"]
            self.assertTrue((target_result == all_data[target_index]).all())
        return time.time() - time_start

    def testFileLoad(self):
        """ 文件加载 """

        target_index, target_result = self.create_file()

        # pickle: load 100 time
        time_list = []
        for i in range(10):
            time_list.append(self.pickle_load(target_index=target_index, target_result=target_result))
        print("pickle load 10 times: {}s per step, max time is {}s, min time is {}s!".format(
            sum(time_list) / len(time_list), max(time_list), min(time_list)))

        # h5py: load 10 time
        time_list = []
        for i in range(10):
            time_list.append(self.h5py_load(target_index=target_index, target_result=target_result))
        print("h5 load 10 times: {}s per step, max time is {}s, min time is {}s!".format(
            sum(time_list) / len(time_list), max(time_list), min(time_list)))

测试结果

文件加载测试结果如下:

Launching unittests with arguments python -m unittest hdf5_benchmark.TestH5 in /mnt/e/frkhit/wsl/tmp/pycharm_benchmark
size of data is 819200112
size of target_result is 90224
pickle file size is 781.25MB
h5 file size is 781.25MB
pickle load 10 times: 2.1771466970443725s per step, max time is 2.5986461639404297s, min time is 2.0592007637023926s!
h5 load 10 times: 0.002041530609130859s per step, max time is 0.004301786422729492s, min time is 0.0013699531555175781s!

结论:

  • h5py不一定能节省空间, 在本测试中, h5py的文件大小与pickle一样
  • h5py在加载数据时, 更省时间(只从硬盘中加载需要的数据)