我正在学习Haskell,并且正在尝试尽可能快地编写代码.在本练习中,我正在为一个简单的一维物理系统编写一个Euler积分器.
-O3.编译的.它运行在1.166秒.-O3.编译.它运行时间为21.3秒.-O3 -fllvm,它运行4.022秒.那么,我是否遗漏了一些优化我的Haskell代码的东西?
PS.:我使用了以下参数:1e-8 5.
C代码:
#include <stdio.h>
double p, v, a, t;
double func(double t) {
return t * t;
}
void euler(double dt) {
double nt = t + dt;
double na = func(nt);
double nv = v + na * dt;
double np = p + nv * dt;
p = np;
v = nv; …Run Code Online (Sandbox Code Playgroud) 我想实现一个特定的算法,但我找不到合适的数据结构.更简单的算法版本如下所示:
Input: A set of points.
Output: A new set of points.
Step 1: For each point, calculate the closest points in a radius.
Step 2: For each point, calculate a value "v" from the closest points subset.
Step 3: For each point, calculate a new value "w" from the closest points and
the values "v" from the previous step, i.e, "w" depends on the neighbors
and "v" of each neighbor.
Step 4: Update points.
Run Code Online (Sandbox Code Playgroud)
在C++中,我可以像这样解决这个问题:
struct Point {
Vector …Run Code Online (Sandbox Code Playgroud)