HDF5-并发、压缩和I/O性能

2024-05-23 08:04:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我对HDF5的性能和并发性有以下问题:

  1. HDF5支持并发写访问吗?
  2. 除了并发性考虑之外,HDF5在I/O性能方面的性能如何(压缩率是否会影响性能)?
  3. 既然我在Python中使用HDF5,它的性能与Sqlite相比如何?

参考文献:


Tags: orglockinghttpsqlitehtmlwww性能参考文献
2条回答

看看pytables,他们可能已经为你做了很多这方面的工作。

也就是说,我不完全清楚如何比较hdf和sqlite。hdf是一种通用的分层数据文件格式+库,sqlite是一个关系数据库。

hdfc级别上确实支持并行I/O,但我不确定这些h5py包了多少,也不知道它是否适合与NFS一起使用。

如果您真的想要一个高度并发的关系数据库,为什么不直接使用一个真正的SQL server呢?

更新为使用pandas 0.13.1

1)编号http://pandas.pydata.org/pandas-docs/dev/io.html#notes-caveats。有多种方法可以做到这一点,例如让不同的线程/进程写出计算结果,然后让单个进程组合起来。

2)根据存储的数据类型、存储方式和检索方式,HDF5可以提供更好的性能。以单个数组的形式存储在HDFStore中,经过压缩(换句话说,不是以允许查询的格式存储)的浮点数据将以惊人的速度存储/读取。即使以表格式存储(这会降低写入性能),也会提供相当好的写入性能。您可以查看这个以获得一些详细的比较(这是HDFStore在引擎盖下使用的)。http://www.pytables.org/,这是一张好照片:

(而且由于PyTables 2.3现在已经为查询编制了索引),所以perf实际上比这要好得多 所以要回答你的问题,如果你想要任何一种性能,HDF5是一条路要走。

写作:

In [14]: %timeit test_sql_write(df)
1 loops, best of 3: 6.24 s per loop

In [15]: %timeit test_hdf_fixed_write(df)
1 loops, best of 3: 237 ms per loop

In [16]: %timeit test_hdf_table_write(df)
1 loops, best of 3: 901 ms per loop

In [17]: %timeit test_csv_write(df)
1 loops, best of 3: 3.44 s per loop

阅读

In [18]: %timeit test_sql_read()
1 loops, best of 3: 766 ms per loop

In [19]: %timeit test_hdf_fixed_read()
10 loops, best of 3: 19.1 ms per loop

In [20]: %timeit test_hdf_table_read()
10 loops, best of 3: 39 ms per loop

In [22]: %timeit test_csv_read()
1 loops, best of 3: 620 ms per loop

这是密码

import sqlite3
import os
from pandas.io import sql

In [3]: df = DataFrame(randn(1000000,2),columns=list('AB'))
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000000 entries, 0 to 999999
Data columns (total 2 columns):
A    1000000  non-null values
B    1000000  non-null values
dtypes: float64(2)

def test_sql_write(df):
    if os.path.exists('test.sql'):
        os.remove('test.sql')
    sql_db = sqlite3.connect('test.sql')
    sql.write_frame(df, name='test_table', con=sql_db)
    sql_db.close()

def test_sql_read():
    sql_db = sqlite3.connect('test.sql')
    sql.read_frame("select * from test_table", sql_db)
    sql_db.close()

def test_hdf_fixed_write(df):
    df.to_hdf('test_fixed.hdf','test',mode='w')

def test_csv_read():
    pd.read_csv('test.csv',index_col=0)

def test_csv_write(df):
    df.to_csv('test.csv',mode='w')    

def test_hdf_fixed_read():
    pd.read_hdf('test_fixed.hdf','test')

def test_hdf_table_write(df):
    df.to_hdf('test_table.hdf','test',format='table',mode='w')

def test_hdf_table_read():
    pd.read_hdf('test_table.hdf','test')

当然是YMMV。

相关问题 更多 >

    热门问题