如果我们想制作一个特定类型的数组类型,使用起来没有问题MakeArrayType()
,例如数组char
:
typeof(char).MakeArrayType()
Run Code Online (Sandbox Code Playgroud)
当然用起来更直观typeof(char[])
。
Assembly
类型的属性告诉我们该类型所在的程序集是什么。
因此,以下代码应该是在程序集中查找类型的合理示例:
var chars=new[] { '\x20' };
var typeofCharArray=chars.GetType();
var assembly=typeofCharArray.Assembly;
var doesContain=assembly.GetTypes().Contains(typeofCharArray);
Run Code Online (Sandbox Code Playgroud)
但doesContain
说它不是,它是false
。无论数组类型是来自MakeArrayType()
or typeof()
,还是实例的 ,都会发生这种情况GetType
。
怀疑它是否被转发到我从Assembly.GetTypes读取的其他程序集。我尝试过:
var assemblyContainsTypeOfCharArray=(
from it in AppDomain.CurrentDomain.GetAssemblies()
let types=it.GetTypes()
where types.Contains(typeof(char[]))
select it).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
有趣的assemblyContainsTypeOfCharArray
是null
。
数组类型在哪里?
我有非常简单的 scala 代码:
def main(): Int = {
var i: Int = 0
var limit = 0
while (limit < 1000000000) {
i = inc(i)
limit = limit + 1
}
i
}
def inc(i: Int): Int = i + 1
Run Code Online (Sandbox Code Playgroud)
我正在使用 JVM JIT 方法内联 inc 方法。当启用内联时,我得到了 2 秒与 4 纳秒的令人惊讶的好例子 - 我想确保或至少验证的是,同时没有循环优化占据宫殿。我看了一下机器代码,看起来没问题
0x000000010b22a4d6: mov 0x8(%rbp),%r10d ; implicit exception: dispatches to 0x000000010b22a529
0x000000010b22a4da: cmp $0xf8033d43,%r10d ; {metadata('IncWhile$')}
0x000000010b22a4e1: jne L0001 ;*iload_3
; - IncWhile$::main@4 (line 7)
0x000000010b22a4e3: cmp $0x3b9aca00,%ebx
0x000000010b22a4e9: …
Run Code Online (Sandbox Code Playgroud) 我试图在内核中加载一个 eBPF 对象libbpf
,但没有成功,出现标题中指定的错误。但让我展示一下我的 BPF*_kern.c
是多么简单。
SEC("entry_point_prog")
int entry_point(struct xdp_md *ctx)
{
int act = XDP_DROP;
int rc, i = 0;
struct global_vars *globals;
struct ip_addr addr = {};
struct some_key key = {};
void *temp;
globals = bpf_map_lookup_elem(&globals_map, &i);
if (!globals)
return XDP_ABORTED;
rc = some_inlined_func(ctx, &key);
addr = key.dst_ip;
temp = bpf_map_lookup_elem(&some_map, &addr);
switch(rc)
{
case 0:
if(temp)
{
// no rocket science here ...
} else
act = XDP_PASS;
break;
default:
break;
}
return act; …
Run Code Online (Sandbox Code Playgroud) 如果我用 C++CLI/托管 C++ 编写程序,编译器是否会执行任何优化?
我知道对于C#来说,有一些优化是在编译时完成的,其中大多数优化是由JIT完成的。C++CLI 也是如此吗?
类似的问题:对于 C++CLI,我可以做相当于 -O2 标志的操作吗?我已经知道“-c Release”标志,但我不清楚它做了什么样的优化。
谢谢!
我将使用 numba 来增强我的代码。然而,并行模式下的幂函数不能很好地工作,即对于以下函数:
import numpy as np
import numba
@numba.njit(parallel=True, fastmath = True)
def decay_rate(mV, mp):
drate=(np.power(mp,-3))
return drate
Run Code Online (Sandbox Code Playgroud)
它说:
指定了关键字参数“parallel=True”,但无法进行并行执行转换。
该函数比上面演示的更复杂(numba 开销值得!)。另外,我**
之前也尝试过电源,但结果是一样的。
我该如何修复它?
我正在模拟一个非常蹩脚的游戏,它基本上会计算玩家在游戏过程中收集的硬币和敌人的数量。该代码包含两个 jitclass:一个player
jitclass 和一个game
jitclass。
对于这个player
类,我们有一些属性和一些方法来描述玩家在游戏中的进展。
from numba import jitclass, int64, float64, deferred_type
from numba.typed import List
import random
specs_player = OrderedDict()
specs_player['level'] = int64
specs_player['coins'] = float64
@jitclass(specs_player)
class Player:
def __init__(self):
self.level = 0
self.coins = 0
self.enemies = List()
def pass_level(self):
self.level += 1
def collect_coins(self, c):
self.coins += c
def collect_enemies(self, e):
self.enemies.append(e)
def reset_player(self):
self.level = 0
self.coins = 0
self.enemies = List()
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该属性enemies
是一个列表,随着玩家在游戏中的进展,该列表会附加值。
jitclass使用前两行将 jitclass 作为属性game
调用: …
这是一个使用辛普森集成代码的简单练习,我已经编写了该代码来接受多个函数以在一组边界上进行集成
\nimport numpy as np\ndef simps(f, a, b, N):\n #N should be even\n dx = (b - a) / N\n x = np.linspace(a, b, N + 1)\n y = f(x)\n w = np.ones_like(y)\n w[2:-1:2] = 2.\n w[1::2] = 4.\n S = dx / 3 * np.einsum("i...,i...",w,y)\n return S\n\ndef funcN(x):\n return np.stack([x**(i/10) * np.exp(-x) for i in range(200)],axis=1)\n\na = np.arange(0,10,0.1)\nb = a+0.05\n
Run Code Online (Sandbox Code Playgroud)\n我在 CPU 设备上,然后我得到一个 200 x 100 数字数组,对应于 \nInt(f_i, a_j,b_j) i:0-199 和 j:0-99
\n%timeit simps(funcN,a,b, 512)
\n …我正在查看我发现的最简单的示例之一,并开始推理SO
(同步顺序)或更准确地说,缺乏同步顺序。考虑下面的例子:
int a, b; // two shared variables
Thread-X:
void threadX() {
synchronized(this) {
a = 1;
}
synchronized(this) {
b = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个读者线程Thread-Y
:
void threadY() {
int r1 = b;
int r2 = a;
}
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我们假设Thread-Y
完全按照这个顺序进行读取:它肯定会先读取b
然后再读取a
(与写入相反)。
允许读取线程查看[1, 0]
(就像之前b=1
发生的那样)。我想我也理解为什么:因为两个操作之间没有同步顺序,因此没有发生之前,并且根据这是一个数据竞争: a=1
JLS
当程序包含两个不按先发生关系排序的冲突访问时,我们称其包含数据争用。
因此,读a
和b
是两个活泼的读法,所以看到b=1
和a=0
是允许的和可能的。
现在这又允许 JVM 在 writer 中进行锁粗化,因此它变成:
void …
Run Code Online (Sandbox Code Playgroud) 安装 Xdebug JIT 后不再工作:
PHP 警告:JIT 与覆盖 zend_execute_ex() 的第三方扩展不兼容。JIT 已禁用。在第 0 行未知
已经卸载了 Xdebug 并重新安装了 PHP,但我仍然收到错误。如何修复它?
apt-get remove --purge php-xdebug
apt-get install --reinstall php8.0-fpm php8.0-apcu php8.0-imap php8.0-curl php8.0-mysql php8.0-mbstring php8.0-bcmath php8.0-xml php8.0-redis php8.0-intl php8.0-soap php8.0-ssh2
Run Code Online (Sandbox Code Playgroud) 我有一个递归函数,它操作整数二叉树,实现为一对嵌套的对或整数。我的函数创建一棵具有不同结构的新树,并递归调用自身直到满足某些条件。我发现的问题是,第一次运行代码时,需要花费很长时间来 JIT 编译该函数的所有可能的签名;之后运行良好。
这是最小的工作示例:
my_tree = ((((6 => 7) => (6 => 7)) => ((7 => 7) => (0 => 7))) => (((8 => 7) => (7 => 7)) => ((8 => 8) => (8 => 0)))) => ((((2 => 4) => 7) => (6 => (0 => 5))) => (((6 => 8) => (2 => 8)) => ((2 => 1) => (4 => 5))))
function tree_reduce(tree::Pair)
left, right = tree
left isa Pair && (left = tree_reduce(left))
right isa …
Run Code Online (Sandbox Code Playgroud)