我发现了这个平均函数的实现:
avg :: [Int] -> Int
avg = div . sum <*> length
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?我查看了由于以下原因产生的函数div . sum:
(div . sum) :: (Integral a, Foldable t) => t a -> a -> a
Run Code Online (Sandbox Code Playgroud)
我明白这一点,但我无法说出<*> length它是如何工作的。
编译以下代码时:
int f(int i1,int i2)
{
long l1=i1;
long l2=i2;
return l1*l2;
}
Run Code Online (Sandbox Code Playgroud)
用clang 10.1forx86-64和 with -O3,我得到
mov eax, edi
imul eax, esi
ret
Run Code Online (Sandbox Code Playgroud)
编译器认识到不需要完整的 64 位操作。
但是,当我用除法替换乘法时:
int f(int i1,int i2)
{
long l1=i1;
long l2=i2;
return l1/l2;
}
Run Code Online (Sandbox Code Playgroud)
它编译成
movsx rax, edi
movsx rsi, esi
cqo
idiv rsi
ret
Run Code Online (Sandbox Code Playgroud)
所以它使用 64 位除法(与 gcc 相同)。
阻止在这里使用 32 位除法的反例是什么?
c compiler-construction optimization clang compiler-optimization
我写了这个小版本的malloc(no free):
#include <cstdio>
#include <cstddef>
#include <unistd.h>
#define word_size sizeof(intptr_t)
#define align(n) ((n + word_size - 1) & ~(word_size - 1))
void* my_malloc(size_t size) {
void* p = sbrk(0);
printf("before allocation: %p\n", p);
if (sbrk(align(size)) == (void*) -1) {
// failed to allocate memory
return NULL;
}
printf("after allocation: %p\n", sbrk(0));
return p;
}
int main() {
int* foo = (int*) my_malloc(1);
*foo = 100;
printf("after allocation outside: %p\n", sbrk(0));
}
Run Code Online (Sandbox Code Playgroud)
这是代码的输出:
before allocation: 0x1796000
after …Run Code Online (Sandbox Code Playgroud) 我很好奇,想知道是否可以让 pyautogui 模块检测图像中每隔几个像素的颜色,并让海龟使用小圆圈在画布中重新创建它们。我最终找到了一种让它工作的方法,而且我喜欢图像的效果!我的代码唯一的问题是需要一段时间才能完成。根据图像的大小,可能需要将近 10-30 分钟才能完成。有什么办法可以优化我的代码吗?我是新手,一直在尝试不同的模块,所以我非常感谢任何建议或帮助!如果你需要我澄清什么,我也很高兴
import time
import turtle
import pyautogui
time.sleep(2)
minimum = pyautogui.position()
print(minimum)
time.sleep(2)
max_x, max_y = pyautogui.position()
print(max_x, max_y)#I use the first point as the top left corner, and the second as the bottom right.
wn = turtle.Screen()
t = turtle.Turtle()
wn.colormode(255)#Allows RGB colors
t.pensize(2)
t.speed(0)
wn.setup(width = 1.0, height = 1.0)
wn.bgcolor("black")
x, y = minimum
min_x, min_y = minimum
def turtlemove(x, y):
t.pu()
t.goto(x - 965, 565 - y)#Compared coordinates in pyautogui with positions …Run Code Online (Sandbox Code Playgroud) 我一直在研究寄存器分配,并想知道为什么当有更好的方法可以做到时,他们都从实时寄存器列表中构建图表。我认为他们可以做到的方式是当活动寄存器超过可用寄存器的数量时,寄存器可能会溢出。这是一个示例(伪组装):
## ldi: load immediate
## addr: add registers and store in arg 2
## store: store memory at offset from stack pointer
.text
main:
# live registers: {}
ldi %t0, 12 # t0 = 12
# live registers: {t0}
ldi %t1, 8 # t1 = 8
# live registers: {t0, t1}
addr %t0, %t1 # t1 = t0 + t1
# live registers: {t1}
store -4(%sp), %t1 # -4(%sp) = t1
# live registers: {}
exit
Run Code Online (Sandbox Code Playgroud)
我已经在汇编代码中列出了实时寄存器。现在,所有的教程和文本都从这里构建了干扰图,等等。但不是这样(正如我上面提到的),他们可以查看活动寄存器。例如,如果这是一台单1寄存器机器,那么当活动寄存器是 …
compiler-construction assembly register-allocation cpu-registers graph-algorithm
最近,我听说 clang 有了一个新功能,_ExtInt. 我知道它可以让你指定一个整数的大小(奇数或什至像 13 位整数),但你如何使用它?
我最近一直在研究 clang 的_ExtInt功能(允许你声明任何大小的 int),只是想知道是否还有一个_ExtFloat我可以用来创建自定义大小的浮点数。
我最近写了一篇小文章malloc,想知道它是否是一个凹凸分配器。我想知道这一点,因为(如果我错了,请纠正我)我相信实际的malloc(使用而mmap不是sbrk)使用相同的技术(某种程度上),但凹凸分配器只会增加堆位置。这是我的代码:
#include <cstdio>
#include <cstddef>
#include <unistd.h>
#define word_size sizeof(intptr_t)
#define align(n) ((n + word_size - 1) & ~(word_size - 1))
void* my_malloc(size_t size) {
void* p = sbrk(0);
if (sbrk(align(size)) == (void*) -1)
return NULL; // failed
return p;
}
int main() {
int* foo = (int*) my_malloc(1);
*foo = 100;
printf("%d\n", *foo);
}
Run Code Online (Sandbox Code Playgroud) 最近,我一直想创建一个小型(教育)功能优化编译器。对于优化部分,我想使用SSA。问题是(据我所知,大多数)函数式编程语言都有不可变变量(默认情况下),因此每个变量只分配一次,就像在 SSA 中一样。需要SSA吗?函数式程序(例如 Haskell 中的程序)是否已经采用 SSA 形式?
compiler-construction optimization functional-programming immutability ssa
c ×5
clang ×3
c++ ×2
extint ×2
malloc ×2
optimization ×2
allocator ×1
applicative ×1
assembly ×1
average ×1
haskell ×1
heap-memory ×1
immutability ×1
integer ×1
memory-leaks ×1
mercury ×1
mode ×1
pointfree ×1
pyautogui ×1
python ×1
python-3.x ×1
ssa ×1
valgrind ×1