我总是假设在做(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)
好像我完全错过了一些东西.有任何想法吗?
据我了解,std::invoke
允许我做类似的事情:
std::invoke(f, arg1, arg2, ...);
Run Code Online (Sandbox Code Playgroud)
是否存在比简单操作更有利的情况:
f(arg1, arg2, ...);
Run Code Online (Sandbox Code Playgroud) 我进行了一个简单的实验,比较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) 我有一个多线程应用程序和共享资源std::map<KeyType, ElementType>
.我使用互斥锁来保护插入,获取和删除.
我的get方法返回对存储元素的引用(返回时解锁),然后我用该元素做一些工作.
问题:在处理存储的元素引用时,是否可能更改另一个线程,std::map
因此元素将被移动到不同的地址,并且引用将不再有效?(我知道某些ADT实现会在调整大小时重新安排ADT).
我在Linux上从源代码安装ac/c ++包时看到了一个常见的模式(Ubuntu 16.04):
我理解make
并且make install
,我猜想configure
根据用户偏好创建一个Makefile,但我不明白为什么autogen.sh
有必要.
有谁知道它的用途是什么?
这就是我想要实现的目标.这很简单:
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) 我想在条件之后对一些 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 中使用(
...)
语法。是(
......)
正确的方法,还是我可能会遗漏一些副作用?
我正在尝试学习分布式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服务器上运行.
有任何想法吗?
我有以下结构的代码:
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停止了内联.
不确定是否需要:我正在使用gcc作为弧形处理器.
我有一个名为Python包mltester
包含两个子包(actions
,dialogs
)和一个主脚本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)