小编bad*_*mad的帖子

为什么rand()+ rand()产生负数?

我观察到rand()库函数在循环中只调用一次,它几乎总是产生正数.

for (i = 0; i < 100; i++) {
    printf("%d\n", rand());
}
Run Code Online (Sandbox Code Playgroud)

但是当我添加两个rand()电话时,现在生成的数字会有更多的负数.

for (i = 0; i < 100; i++) {
    printf("%d = %d\n", rand(), (rand() + rand()));
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么我在第二种情况下看到负数吗?

PS:我在循环之前初始化种子为srand(time(NULL)).

c random

302
推荐指数
3
解决办法
4万
查看次数

如何用char数组打包短整数的位字段?

在以下2个结构中,

typedef struct _a {
    short a1:13 __attribute__((packed));
    char a2[4]  __attribute__((packed));
} a;

typedef struct _b {
    short b1:10 __attribute__((packed));
    short b2:10 __attribute__((packed));
    short b3:12 __attribute__((packed));
} b;
Run Code Online (Sandbox Code Playgroud)

struct b,我发现b2的位用b1打包,b3的位用b2打包.它最终导致4字节值.

我期待类似的行为,struct a但我看不出相同.前2个字节用a1(未使用的5位)占用,后跟4个字节用于a2.

这种行为有望吗?为什么我不能将char [4]与短片一起包装:13?有没有办法实现它?

c bit-packing bit-fields

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

Linux 性能记录不生成任何样本

我正在尝试在具有 PMU 支持的 aria10 fpga 板(带有 2 个 ARM Cortex A9 CPU)上分析我的用户空间程序。我正在运行 windriver linux 版本 9.x。我使用人们在互联网上建议的几乎所有 CONFIG_ 选项构建了我的内核。另外,我的 pgm 是使用 \xe2\x80\x93fno-omit-frame-pointer 和 \xe2\x80\x93g 选项编译的。

\n\n

我看到的是 \xe2\x80\x98perf record\xe2\x80\x99 根本不生成任何样本。\xe2\x80\x98perf stat true\xe2\x80\x99 输出看起来是有效的(不知道从中可以得到什么)。有人有建议/想法为什么我没有看到生成任何样本吗?

\n\n
~: perf record --call-graph dwarf --  my_app\n\n^C\n[ perf record: Woken up 1 times to write data ]\n[ perf record: Captured and wrote 0.003 MB perf.data ]\n\n~: perf report -g graph --no-children\nError:\nThe perf.data file has no samples!\n To display the perf.data header info, please use --header/--header-only options.\n\n~: perf …
Run Code Online (Sandbox Code Playgroud)

linux arm perf

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

python3性能:循环使用range()Vs plain old while()

在Python3中,我发现如果我替换一个

对于范围内的i(n):

声明

而我<n:

我获得了显着的运行时增益 我的循环本身并没有多余的基础算术运算.

任何指向我为什么看到这种行为的指针?

编辑:n的范围是K,10K,12K等10s.我观察到的时间是.19s为12K,.12s为10K with while循环.使用'while'循环,我看到.11s为12K,.08s为10K.这是我的计划:

target = 0
i = 1
#for i in range(1, n+1):
while i < n+1:
    target += i * (2 ** (i - 1)) + (i * (i + 1))//2
    i += 1

return target % (10 ** 9 + 7)
Run Code Online (Sandbox Code Playgroud)

python performance range python-3.x

0
推荐指数
1
解决办法
60
查看次数

标签 统计

c ×2

arm ×1

bit-fields ×1

bit-packing ×1

linux ×1

perf ×1

performance ×1

python ×1

python-3.x ×1

random ×1

range ×1