小编Pin*_*_be的帖子

为什么uint64_t在类中使用时需要比2 uint32_t更多的内存?以及如何防止这种情况?

我将以下代码作为示例.

#include <iostream>

struct class1
{

    uint8_t     a;
    uint8_t     b;
    uint16_t    c;
    uint32_t    d;
    uint32_t    e;

    uint32_t    f;
    uint32_t    g;
};

struct class2
{

    uint8_t     a;
    uint8_t     b;
    uint16_t    c;
    uint32_t    d;
    uint32_t    e;

    uint64_t    f;
};

int main(){
    std::cout << sizeof(class1) << std::endl;
    std::cout << sizeof(class2) << std::endl;
    std::cout << sizeof(uint64_t) << std::endl;
    std::cout << sizeof(uint32_t) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

版画

20
24
8
4
Run Code Online (Sandbox Code Playgroud)

所以很容易看出一个uint64_t和两个uint32_t一样大,为什么类2有4个额外的字节,如果它们是相同的,除了替换两个uint32_t用于uint64_t.

c++ size class uint64

6
推荐指数
1
解决办法
1209
查看次数

如何从python文件获取类和函数的列表而不导入

我有一个python文件,其中定义了一些类和函数:

class A(object):
    def __init__(self, an_arg, a_default_arg=None):
        pass

def doStuff(an_other_arg, an_other_default_arg=None):
    pass
Run Code Online (Sandbox Code Playgroud)

我想获得此文件中所有类和函数的列表。(它们的名称和参数定义就足够了)

现在,我知道您可以使用__import__(module_descriptor)和进行此操作inspect,但这不是一个选择,因为我正在扫描的文件来自不受信任的来源。

我的第一个反应是尝试创建一个安全的环境来导入它们,但是根据其他stackoverflow问题,这似乎是不可能的。

python class python-import

5
推荐指数
1
解决办法
1406
查看次数

有效地从包含python中的大量文件的zip中读取一个文件

我将索引存储在磁盘上的压缩zip中,并希望从此zip文件中提取单个文件.在python中执行此操作似乎非常慢,是否可以解决此问题.

with zipfile.ZipFile("testoutput/index_doc.zip", mode='r') as myzip:
    with myzip.open("c0ibtxf_i.txt") as mytxt:
        txt = mytxt.read()
        txt = codecs.decode(txt, "utf-8")
        print(txt)
Run Code Online (Sandbox Code Playgroud)

是我使用的python代码.在python中运行此脚本需要相当长的时间

python3 testunzip.py  1.22s user 0.06s system 98% cpu 1.303 total
Run Code Online (Sandbox Code Playgroud)

这很烦人,特别是因为我知道它可以更快:

unzip -p testoutput/index_doc.zip c0ibtxf_i.txt  0.01s user 0.00s system 69% cpu 0.023 total
Run Code Online (Sandbox Code Playgroud)

根据要求:分析

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.051    0.051    1.492    1.492 <string>:1(<module>)
127740    0.043    0.000    0.092    0.000 cp437.py:14(decode)
     1    0.000    0.000    1.441    1.441 testunzip.py:69(toprofile)
     1    0.000    0.000    0.000    0.000 threading.py:72(RLock)
     1    0.000    0.000 …
Run Code Online (Sandbox Code Playgroud)

python performance unzip zipfile python-3.x

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

标签 统计

class ×2

python ×2

c++ ×1

performance ×1

python-3.x ×1

python-import ×1

size ×1

uint64 ×1

unzip ×1

zipfile ×1