小编ask*_*djd的帖子

在Python中将字符串转换为ctypes.c_ubyte数组的有效方法

我有一个20字节的字符串,我想将其转换为ctypes.c_ubyte数组以进行位字段操作.

 import ctypes
 str_bytes = '01234567890123456789'
 byte_arr = bytearray(str_bytes)
 raw_bytes = (ctypes.c_ubyte*20)(*(byte_arr))
Run Code Online (Sandbox Code Playgroud)

有没有办法避免为了演员而从str到bytearray的深拷贝?

或者,是否可以在没有深层复制的情况下将字符串转换为bytearray?(使用memoryview等技术?)

我使用的是Python 2.7.

表现结果:

使用eryksunBrian Larsen的建议,这里是使用Ubuntu 12.04和Python 2.7的vbox VM下的基准测试.

  • method1使用我的原始帖子
  • method2使用ctype from_buffer_copy
  • method3使用ctype cast/POINTER
  • method4使用numpy

结果:

  • method1需要3.87秒
  • method2需要0.42秒
  • method3需要1.44秒
  • method4需要8.79秒

码:

import ctypes
import time
import numpy

str_bytes = '01234567890123456789'

def method1():
    result = ''
    t0 = time.clock()
    for x in xrange(0,1000000):     
        byte_arr = bytearray(str_bytes)
        result = (ctypes.c_ubyte*20)(*(byte_arr))

    t1 = time.clock()
    print(t1-t0)

    return result

def method2():

    result = ''
    t0 = …
Run Code Online (Sandbox Code Playgroud)

python ctypes bytearray

8
推荐指数
1
解决办法
1万
查看次数

无法启动HBase独立模式

我正在阅读HBase QuickStart指南(http://hbase.apache.org/book/quickstart.html),我遇到了很多问题,完成了第一步.

我在VirtualBox下使用Mint Linux 13,Win7作为主机.

我下载了hbase 0.94.6.1,在我的主路径上解压缩文件,配置了环回地址.我写入/ tmp用于测试目的很好,所以我没有修改/conf/hbase-site.xml.

start-hbase.sh: 45: [: false: unexpected operator
localhost: starting zookeeper, logging to /home/askldjd/hbase-0.94.6.1/bin/../logs/hbase-askldjd-zookeeper-test-hadoop.out
starting master, logging to /home/askldjd/hbase-0.94.6.1/bin/../logs/hbase-askldjd-master-test-hadoop.out
Could not start ZK at requested port of 2181.  ZK was started at port: 2182.  Aborting as clients (e.g. shell) will not be able to find this ZK quorum.
localhost: starting regionserver, logging to /home/askldjd/hbase-0.94.6.1/bin/../logs/hbase-askldjd-regionserver-test-hadoop.out
Run Code Online (Sandbox Code Playgroud)

如果我输入./bin/hbase shell,并输入status,这就是我得到的.

13/04/05 01:47:06 ERROR client.HConnectionManager$HConnectionImplementation: Check the value configured in 'zookeeper.znode.parent'. There could be a mismatch with …
Run Code Online (Sandbox Code Playgroud)

hbase

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

bytearray ×1

ctypes ×1

hbase ×1

python ×1