我有一些或多或少像这样的代码:
#include <bitset>
enum Flags { A = 1, B = 2, C = 3, D = 5,
E = 8, F = 13, G = 21, H,
I, J, K, L, M, N, O };
void apply_known_mask(std::bitset<64> &bits) {
const Flags important_bits[] = { B, D, E, H, K, M, L, O };
std::remove_reference<decltype(bits)>::type mask{};
for (const auto& bit : important_bits) {
mask.set(bit);
}
bits &= mask;
}
Run Code Online (Sandbox Code Playgroud)
Clang> = 3.6做聪明的事情并将其编译为单个and指令(然后在其他地方内联):
apply_known_mask(std::bitset<64ul>&): # @apply_known_mask(std::bitset<64ul>&)
and qword …Run Code Online (Sandbox Code Playgroud) 考虑以下代码片段:
int* find_ptr(int* mem, int sz, int val) {
for (int i = 0; i < sz; i++) {
if (mem[i] == val) {
return &mem[i];
}
}
return nullptr;
}
Run Code Online (Sandbox Code Playgroud)
-O3上的GCC将其编译为:
find_ptr(int*, int, int):
mov rax, rdi
test esi, esi
jle .L4 # why not .L8?
lea ecx, [rsi-1]
lea rcx, [rdi+4+rcx*4]
jmp .L3
.L9:
add rax, 4
cmp rax, rcx
je .L8
.L3:
cmp DWORD PTR [rax], edx
jne .L9
ret
.L8:
xor eax, eax
ret
.L4: …Run Code Online (Sandbox Code Playgroud) 我在我的一个项目中使用C++ 11,并且想知道如何最好地表示ELF幻数.我不是十六进制文字的粉丝,所以我一直在寻找比以下更好的东西:
const uint32 ELF_MAGIC_NUMBER = 0x7F454c46; // 0x7F, E, L, F
Run Code Online (Sandbox Code Playgroud)
所以,我试着写:
const uint32 ELF_MAGIC_NUMBER = { 0x7F, 'E', 'L', 'F' };
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨初始化列表中的项目太多,这是可以理解的,虽然很烦人.
有没有办法按字节写一个整数文字?我觉得第一个选项,虽然它有效,但在第二个选项上并不可读.
我正在尝试将以下MATLAB代码转换为Python,并且无法找到可在任何合理时间内工作的解决方案.
M = diag(sum(a)) - a;
where = vertcat(in, out);
M(where,:) = 0;
M(where,where) = 1;
Run Code Online (Sandbox Code Playgroud)
这里,a是稀疏矩阵,其中是矢量(如输入/输出).我使用Python的解决方案是:
M = scipy.sparse.diags([degs], [0]) - A
where = numpy.hstack((inVs, outVs)).astype(int)
M = scipy.sparse.lil_matrix(M)
M[where, :] = 0 # This is the slowest line
M[where, where] = 1
M = scipy.sparse.csc_matrix(M)
Run Code Online (Sandbox Code Playgroud)
但由于A是334863x334863,这需要三分钟.如果有人对如何加快速度有任何建议,请提供帮助!为了进行比较,MATLAB在不知不觉中快速完成了同样的步骤.
谢谢!
这更像是一种好奇心,但我想知道是否有可能在编译后应用编译器优化.大多数优化技术是否高度依赖于IR,或者组装是否可以相当容易地来回转换?
我的一位学生向我展示了以下测试用例,该用例显示了 NumPy 中明显的内存泄漏。我想知道内存分析器是否正确,或者发生了什么。这是测试用例:
from memory_profiler import profile
import numpy as np
import gc
@profile
def test():
arr = np.ones((10000, 6912))
for i in range(2000):
arr[0:75,:] = np.ones((75, 6912))
del arr
gc.collect()
pass
test()
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
Filename: test.py
Line # Mem usage Increment Occurences Line Contents
============================================================
5 32.9 MiB 32.9 MiB 1 @profile
6 def test():
7 560.3 MiB 527.4 MiB 1 arr = np.ones((10000, 6912))
8 564.2 MiB 0.0 MiB 2001 for i in range(2000):
9 564.2 MiB 3.9 MiB …Run Code Online (Sandbox Code Playgroud) 我正在尝试在python 3中使用键盘库,但继续导致导入错误.我在Thonny的Windows中运行程序,它工作正常,但我不能在pi中运行它.我尝试以root身份和sudo命令运行它,结果相同.下面是代码和错误.
import keyboard
import time
x=0
while True:
print (x)
x=x+1
print ("Press t to end program")
if keyboard.is_pressed('t'):
break
else:
pass
print("I'm done")
Run Code Online (Sandbox Code Playgroud)
产量
0
Press t to end program
Traceback (most recent call last):
File "/home/pi/Desktop/Programs/KeyboardTest.py", line 10, in <module>
if keyboard.is_pressed('t'):
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/__init__.py", line 166, in is_pressed
_listener.start_if_necessary()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/__init__.py", line 116, in init
_os_keyboard.init()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_nixkeyboard.py", line 110, in init
build_device()
File "/home/pi/.local/lib/python3.5/site-packages/keyboard/_nixkeyboard.py", line 106, in build_device
ensure_root() …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,它发出一系列字符串以匹配Excel上的标题.如果您不熟悉Excel,则该序列如下所示:
A,B,...,Z,AA,...,AZ,BA,...,ZZ,AAA,...,etc.
Run Code Online (Sandbox Code Playgroud)
这是我提出的代码:
function next(id) {
if(id === "")
return "A";
var prefix = id.substring(0, id.length-1);
var last = id[id.length-1]
if(last === "Z")
return (next(prefix) + "A");
return prefix + String.fromCharCode(id.charCodeAt(id.length-1) + 1);
}
Run Code Online (Sandbox Code Playgroud)
你知道更好/更清洁的方式吗?
我见过以下所有问题:
尽管遵循了他们的所有建议并在我的所有测试项目中都有以下几行:
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
Run Code Online (Sandbox Code Playgroud)
我仍然在ReSharper(版本2018.1.2)的单元测试资源管理器窗口中收到以下错误:
2018.06.23 11:22:21.292 ERROR Transition failed: Transition from state <HandShake> on event <remote::.ProtocolVersion>. Cause: System.InvalidOperationException: Test-cases are missing for the selected tests. Rebuild the project and try again. at JetBrains.ReSharper.UnitTestFramework.DotNetCore.DotNetVsTest.DotNetVsTestExecution.SendGetProcessStartInfo() at Appccelerate.StateMachine.Machine.ActionHolders.ArgumentLessActionHolder.Execute(Object argument) at Appccelerate.StateMachine.Machine.States.State`2.ExecuteEntryAction(IActionHolder actionHolder, ITransitionContext`2 context)
--- EXCEPTION #1/1 [LoggerException]
Message = “
Transition failed: Transition from state <HandShake> on event <remote::.ProtocolVersion>.
Cause: System.InvalidOperationException: Test-cases are missing …Run Code Online (Sandbox Code Playgroud)