我正在尝试优化我的多项式实现.特别是我正在处理具有模数n(可能是>2^64)的多项式,并以形式x^r - 1(r是< 2^64)对多项式求模.目前我将系数表示为整数列表(*),并且我以最直接的方式实现了所有基本操作.
我希望取幂和乘法尽可能快,为了获得这个,我已经尝试了不同的方法.我目前的方法是将系数列表转换为大整数乘以整数并将系数解包.
问题是包装和拆包需要花费很多时间.
那么,有没有办法改善我的"打包/解包"功能?
def _coefs_to_long(coefs, window):
'''Given a sequence of coefficients *coefs* and the *window* size return a
long-integer representation of these coefficients.
'''
res = 0
adder = 0
for k in coefs:
res += k << adder
adder += window
return res
#for k in reversed(coefs): res = (res << window) + k is slower
def _long_to_coefs(long_repr, window, n):
'''Given a long-integer representing coefficients …Run Code Online (Sandbox Code Playgroud) 鉴于此代码段:
someFunction x = print x `seq` 1
main = do print (someFunction "test")
Run Code Online (Sandbox Code Playgroud)
代码执行时为什么不print x打印test?
$./seq_test
1
Run Code Online (Sandbox Code Playgroud)
如果我替换它error我可以检查左操作数seq 是否确实被评估.
我怎样才能达到预期的输出:
test
1
Run Code Online (Sandbox Code Playgroud)
只修改someFunction?
我正在尝试使用以下函数实现二进制搜索:
def buggy_binary_search(input, key):
low = 0
high = len(input)-1
mid = (low + high)/2
while low <= high:
if input[mid] == key:
return mid
if input[mid] > key:
high = mid - 1
else:
low = mid
return -1
Run Code Online (Sandbox Code Playgroud)
运行时的上述函数进入无限循环.我怎么能纠正这个?
我遇到过一些包含特别奇怪的导入的模块.
首先,我看到一个模块A将自己的其他模块导入.例如:
-- module A.hs
module A where
import B as A -- ???
f = id
Run Code Online (Sandbox Code Playgroud)
这是做什么的?为什么上面允许这样做?
然而,最困扰我的是代码实际上是这样的:
module A where
import B as A -- Okay, assume this works...
import C as A -- ??? A is already defined!
f = id
Run Code Online (Sandbox Code Playgroud)
为什么可以使用相同名称导入多个模块?这实现了什么?我认为import不允许使用这些类型,而且Haskell的温和介绍说:
将具有相同名称的两个不同实体导入同一范围是非法的.
但是这些进口工作正常.然而另一件令我烦恼的奇怪事情就是导出模块本身:
module A (module A) where
Run Code Online (Sandbox Code Playgroud)
总结一下,鉴于以下MWE:
-- A.hs
module A (module A) where
import B as A
import C as A
f = id
-- B.hs
module …Run Code Online (Sandbox Code Playgroud) 我注意到我通常构建使用镜头获取值的函数,将一些函数应用于值并返回结果.例如,将一对元素相加
\pair -> (pair ^. _1) + (pair ^. _2)
我觉得应该有一些组合器来组合getters第一类并返回另一个getter(可能是类型(b -> c -> d) -> Getter a b -> Getter a c -> Getter a d).有帮助吗?
在GHC 7.10.1和cabal 1.23下建议的分析程序是什么?来自GHC和cabal-install的与性能分析相关的错误和警告消息非常不一致.
尝试使用性能分析运行可执行文件,然后告诉您:
$ prediction-interactive +RTS -p
prediction-interactive: the flag -p requires the program to be built with -prof
Run Code Online (Sandbox Code Playgroud)放入-prof你的cabal文件,你会被告知:
$ cabal build
Warning: 'ghc-options: -prof' is not necessary and will lead to problems when
used on a library. Use the configure flag --enable-library-profiling and/or
--enable-executable-profiling.
Run Code Online (Sandbox Code Playgroud)遵循该建议(并从cabal文件中删除-prof),并告诉您:
$ cabal configure --enable-executable-profiling
Resolving dependencies...
Configuring creatur-wains-prediction-1.0...
Warning: The flag --enable-executable-profiling is deprecated. Please use
--enable-profiling instead.
Run Code Online (Sandbox Code Playgroud)遵循这个建议,你被告知:
$ cabal configure --enable-profiling
Resolving dependencies...
Configuring creatur-wains-prediction-1.0...
$ cabal build …Run Code Online (Sandbox Code Playgroud)通常Haskell标签中的问题是为什么haskell与X相比如此之慢.大多数情况下,你可以将它与使用String而不是Text或用来联系起来ByteString.非严格评估或缺乏类型签名.
但是在这里,我有一个简单的斐波那契计算器,其性能优于C++约2倍.这可能是缺乏c ++知识 - 但我从一位朋友那里得到了代码,他过去常常用这种语言编写代码.
? g++ -O3 fib2.cc -o cc-fib -lgmpxx -lgmp
? time ./cc-fib > /dev/null
./cc-fib > /dev/null 8,23s user 0,00s system 100% cpu 8,234 total
? ghc -O3 --make -o hs-fib fib1.hs
[1 of 1] Compiling Main ( fib1.hs, fib1.o )
Linking hs-fib ...
? time ./hs-fib > /dev/null
./hs-fib > /dev/null 4,36s user 0,03s system 99% cpu 4,403 total
Run Code Online (Sandbox Code Playgroud)
在haskell文件中,我只使用了strict zipWith'和strict add'函数(这是使用扩展的地方BangPatterns- 它只是告诉编译器在执行添加之前评估参数x/ …
我有一个树状的结构.此结构中的每个元素都应该能够返回它所属的所有元素的Enumerable.我们称之为这种方法IEnumerable<Foo> GetAll().所以,如果我们有
A <-- topmost root
/ \
B C
/ \ / \
D E F G
Run Code Online (Sandbox Code Playgroud)
GetAll对元素C返回的调用{C, F, G}(元素的固定顺序很好,但不需要).我想每个人都已经知道了.
目前的实现GetAll看起来像这样:
public IEnumerable<Foo> GetAll ()
{
yield return this;
foreach (Foo foo in MyChildren) {
foreach (Foo f in foo.GetAll ()) {
yield return f;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在早期的实现中,我返回了一个List并添加了child-foos List.AddRange().
我的问题是,是否正确实施了使用产量的版本,或者是否应该改进(特别是在性能方面).或者这只是坏事我应该坚持Lists(或ReadOnlyCollections)而不是?
我正在编写一个小型的C客户端/服务器应用程序,但在使用外部IP地址时无法使连接正常工作.客户端和服务器的代码都来自这里,特别是客户端:
char *default_server_name = "localhost";
char *server_name = NULL;
int nport = DEFAULT_DAMA_PORT;
char port[15];
// Parse the command line options
if (parse_options(argc, argv, &server_name, &nport) < 0) {
return -1;
}
if (server_name == NULL) {
server_name = default_server_name;
}
snprintf(port, 15, "%d", nport);
// Connect to the server
int client_socket;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(server_name, port, &hints, &servinfo)) != 0) { …Run Code Online (Sandbox Code Playgroud) 因此在iOS Swift中,我们可以执行可选链接来简化nil检查,就像在官方文档中一样
let johnsAddress = Address()
johnsAddress.buildingName = "The Larches"
johnsAddress.street = "Laurel Street"
john.residence!.address = johnsAddress
if let johnsStreet = john.residence?.address?.street {
println("John's street name is \(johnsStreet).")
} else {
println("Unable to retrieve the address.")
}
// prints "John's street name is Laurel Street."
Run Code Online (Sandbox Code Playgroud)
我了解在选购链接的使用john.residence?.address?.street,但我们怎样才能知道实际链条断裂(如果任一residence或者address是nil).我们可以判断residence或者address是nil,或者我们需要与检查一遍if-else?
haskell ×5
performance ×2
python ×2
c ×1
c# ×1
c++ ×1
getaddrinfo ×1
ghc ×1
haskell-lens ×1
ienumerable ×1
import ×1
io ×1
optimization ×1
optional ×1
profiling ×1
seq ×1
sockets ×1
swift ×1
yield ×1