小编Ela*_*iss的帖子

为什么(a%256)与(a&0xFF)不同?

我总是假设在做(a % 256)优化器时自然会使用有效的按位操作,就像我写的那样(a & 0xFF).

在编译器资源管理器gcc-6.2(-O3)上进行测试时:

// Type your code here, or load an example.
int mod(int num) {
    return num % 256;
}

mod(int):
    mov     edx, edi
    sar     edx, 31
    shr     edx, 24
    lea     eax, [rdi+rdx]
    movzx   eax, al
    sub     eax, edx
    ret
Run Code Online (Sandbox Code Playgroud)

在尝试其他代码时:

// Type your code here, or load an example.
int mod(int num) {
    return num & 0xFF;
}

mod(int):
    movzx   eax, dil
    ret
Run Code Online (Sandbox Code Playgroud)

好像我完全错过了一些东西.有任何想法吗?

c++ optimization

143
推荐指数
4
解决办法
2万
查看次数

何时使用std :: invoke而不是简单地调用invokable?

据我了解,std::invoke允许我做类似的事情:

std::invoke(f, arg1, arg2, ...);
Run Code Online (Sandbox Code Playgroud)

是否存在比简单操作更有利的情况:

f(arg1, arg2, ...);
Run Code Online (Sandbox Code Playgroud)

c++ std c++17

13
推荐指数
2
解决办法
1724
查看次数

是否比if + default更快?

我进行了一个简单的实验,比较if-else到只有if(预设默认值).例:

void test0(char c, int *x) {
    *x = 0;
    if (c == 99) {
        *x = 15;
    }
}

void test1(char c, int *x) {
    if (c == 99) {
        *x = 15;
    } else {
        *x = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

对于上面的函数,我得到了完全相同的汇编代码(使用cmovne).

但是在添加额外变量时:

void test2(char c, int *x, int *y) {
    *x = 0;
    *y = 0;
    if (c == 99) {
        *x = 15;
        *y = 21;
    }
}

void test3(char c, int *x, int *y) …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization assembly

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

可以对std :: map中的元素引用无效吗?

我有一个多线程应用程序和共享资源std::map<KeyType, ElementType>.我使用互斥锁来保护插入,获取和删除.

我的get方法返回对存储元素的引用(返回时解锁),然后我用该元素做一些工作.

问题:在处理存储的元素引用时,是否可能更改另一个线程,std::map因此元素将被移动到不同的地址,并且引用将不再有效?(我知道某些ADT实现会在调整大小时重新安排ADT).

c++ multithreading std c++11

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

在Linux上构建c ++包时,autogen.sh的工作是什么

我在Linux上从源代码安装ac/c ++包时看到了一个常见的模式(Ubuntu 16.04):

  1. ./autogen.sh
  2. ./配置
  3. 使
  4. make install

我理解make并且make install,我猜想configure根据用户偏好创建一个Makefile,但我不明白为什么autogen.sh有必要.

有谁知道它的用途是什么?

c c++ makefile configure

8
推荐指数
2
解决办法
4634
查看次数

根据条件然后三元运算符获得值的更快方法?

这就是我想要实现的目标.这很简单:

unsigned int foo1(bool cond, unsigned int num)
{
    return cond ? num : 0;
}
Run Code Online (Sandbox Code Playgroud)

Assmebly:

    test    dil, dil
    mov     eax, 0
    cmovne  eax, esi
    ret
Run Code Online (Sandbox Code Playgroud)

我的问题是,有更快的方法吗?以下是我想到的一些方法:

使用乘法:

unsigned int foo2(bool cond, unsigned int num)
{
    return cond * num;
}
Run Code Online (Sandbox Code Playgroud)

ASSMBLY:

    movzx   eax, dil
    imul    eax, esi
    ret
Run Code Online (Sandbox Code Playgroud)

使用内存访问:

unsigned int foo3(bool cond, unsigned int num)
{
    static const unsigned int masks[2] = { 0x0, 0xFFFFFFFF };
    return masks[cond] & num;
}
Run Code Online (Sandbox Code Playgroud)

部件:

    movzx   edi, dil
    mov     eax, …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization

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

在 bash 中进行命令分组的正确方法是什么

我想在条件之后对一些 bash 指令进行分组:

第一次尝试:

$ [[ 0 == 1 ]] && echo 1; echo 2
2
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

$ [[ 0 == 1 ]] && (echo 1; echo 2)
$ [[ 0 == 0 ]] && (echo 1; echo 2)
1
2
Run Code Online (Sandbox Code Playgroud)

所以后者是我想要的。

问题:这是我第一次在 bash 中使用(...)语法。是(......)正确的方法,还是我可能会遗漏一些副作用?

bash

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

TensorFlow可以运行多个CPU(没有GPU)吗?

我正在尝试学习分布式TensorFlow.试过这里解释的片段代码:

with tf.device("/cpu:0"):
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))

with tf.device("/cpu:1"):
    y = tf.nn.softmax(tf.matmul(x, W) + b)
    loss = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
Run Code Online (Sandbox Code Playgroud)

收到以下错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'MatMul': Operation was explicitly assigned to /device:CPU:1 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
     [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/device:CPU:1"](Placeholder, Variable/read)]]

意味着TensorFlow无法识别CPU:1.

我正在使用40个CPU(cat /proc/cpuinfo | grep processor | wc -l)的RedHat服务器上运行.

有任何想法吗?

python redhat python-2.7 tensorflow

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

函数停止内联.任何解释?

我有以下结构的代码:

void foo1(uint32_t *num_failures)
{
   ...
}

void foo2(uint32_t *num_failures)
{
   ...
}

void foo3(uint32_t *num_failures)
{
   ...
}

void test()
{
   uint32_t num_failures = 0;
   foo1(&num_failures);
   foo2(&num_failures);
   foo3(&num_failures);
}
Run Code Online (Sandbox Code Playgroud)

现在,我所做的是将以下指令添加到foo1():

void foo1(uint32_t *num_failures)
{
   ...
   (*num_failures)++;
}
Run Code Online (Sandbox Code Playgroud)

突然间,我看到从foo2()内部打印的堆栈大小增加了36个字节.

我做了一个objdump并为<symbols>加油.产生以下内容:

改变之前:

...
00004e08 <test>:
Run Code Online (Sandbox Code Playgroud)

改变后:

...
00004e08 <foo2>:
00005588 <test>:
Run Code Online (Sandbox Code Playgroud)

所以我猜函数foo2停止了内联.

  1. 我对么?
  2. 有没有解释为什么会这样?
  3. 改变之后foo3()发生了什么?它是否在foo2()内部或在test()内部内联?

不确定是否需要:我正在使用gcc作为弧形处理器.

c c++ stack gcc inline

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

为什么我需要在setup.py中包含子软件包

我有一个名为Python包mltester包含两个子包(actionsdialogs)和一个主脚本ml_tester.py,结构如下:

+ <ProjectFolder>
+---+ <mltester>
|   +---- <actions>
|   +---- <dialogs>
|   +---- ml_tester.py
|   +---- __init__.py
+---- setup.py
Run Code Online (Sandbox Code Playgroud)

我的__init__.py样子如下:

import actions
import dialogs
import ml_tester
Run Code Online (Sandbox Code Playgroud)

ml_tester.py我做类似的事情:

from actions import *
from dialogs import *
Run Code Online (Sandbox Code Playgroud)

从Eclipse运行时,一切正常。这样做时pip install,以下setup.py工作正常:

from setuptools import setup
setup(
    name="MLTester",
    version="1.0",
    packages=["mltester",
              "mltester.actions",
              "mltester.dialogs"],
    install_requires=[
        "matplotlib",
    ],
    entry_points='''
        [console_scripts]
        ml_tester_gui=mltester.ml_tester:main
    '''
)
Run Code Online (Sandbox Code Playgroud)

但是,当我从软件包列表中删除时"mltester.actions""mltester.dialogs"现在出现如下错误:

File "/usr/local/lib/python2.7/dist-packages/mltester/__init__.py", line 1, …
Run Code Online (Sandbox Code Playgroud)

python setup.py

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