小编bra*_*emp的帖子

为什么Haskell列表推导不是并行执行的?

我正在做项目欧拉问题21的家庭作业,我有这个列表理解:

amicableNumberSums = [ x+y | x<-[1..10000], y <-[1..10000], (amicable x y)]
Run Code Online (Sandbox Code Playgroud)

这需要很长时间才能执行(可以理解,因为它测试10000 ^ 2对数字)并查看我的cpu使用情况,它表明只使用了1个核心.

由于列表理解没有副作用,因此同时测试多对数字没有危险.有没有办法让Haskell自动执行此操作,或者如果不能如何修改我的代码来执行此操作?

(编辑)运行print时出错(usingamicableNumberSums parList):

Couldn't match type `a0 -> Eval a0' with `[Int]'
Expected type: Strategy [Int]
  Actual type: Strategy a0 -> Strategy [a0]
In the second argument of `using', namely `parList'
In the first argument of `print', namely
  `(amicableNumberSums `using` parList)'
In the expression: print (amicableNumberSums `using` parList)
Run Code Online (Sandbox Code Playgroud)

(编辑)两种建议方法的表现:

Ørjan Johansen's method: 218.79s elapsed parallel (4 cores + 4 hyperthreading)
                         279.37s elapsed sequential …
Run Code Online (Sandbox Code Playgroud)

parallel-processing haskell list-comprehension

8
推荐指数
2
解决办法
1033
查看次数

Python的'a +'文件打开模式中的错误?

我目前正在使用python-fuse创建一个文件系统,并且查找文件指针在每个不同模式('r','r +'等)的起始位置,并在多个站点上找到文件指针从零开始,除非当它从文件末尾开始时,它在'a'或'a +'中打开.

我在Python中测试了这个以确保(在每个模式中打开一个文本文件并立即调用tell())但发现当它在'a +'中打开时,文件指针为零而不是文件的末尾.

这是python中的错误,还是网站错了?

以供参考:

python file-io file mode python-2.7

3
推荐指数
1
解决办法
1176
查看次数

为什么反映Type.Implements()比类型断言慢得多?

我正在尝试有效地测试接口{}是否实现给定的函数,我的解决方案是创建一个只有这个函数的接口,然后检查接口{}是否实现了这个单一的函数接口.这里的两个选项似乎是使用反射或类型断言.两者似乎都具有相同的行为,但速度差异很大.

查看Value.Implements()的代码,它对值上定义的函数进行线性扫描,并将它们与接口进行比较.然而,类型断言似乎只进行了一个恒定的时间比较(与接口中的函数数量无关).

Implements()不只是做一个类型断言是有原因的吗?

基准测试:

package benchmarks

import (
    "reflect"
    "testing"
)

type ITest interface {
    Foo()
}

type Base struct{}

func (Base) A() {}
func (Base) B() {}
func (Base) C() {}
func (Base) D() {}
func (Base) E() {}
func (Base) F() {}
func (Base) G() {}
func (Base) H() {}
func (Base) I() {}
func (Base) J() {}

var Interface = reflect.TypeOf((*ITest)(nil)).Elem()

func BenchmarkReflection(b *testing.B) {
    var iface interface{}
    iface = Base{}
    for i := 0; i < …
Run Code Online (Sandbox Code Playgroud)

reflection go

3
推荐指数
1
解决办法
1585
查看次数