小编Pau*_* M.的帖子

改进D中的逐行I/O操作

我需要以行方式处理大量的中型到大型文件(几百MB到GB),所以我对用于迭代线的标准D方法感兴趣.这个foreach(line; file.byLine())成语似乎符合要求,并且令人愉快,简洁易读,但性能似乎不太理想.

例如,下面是Python和D中的两个简单程序,用于迭代文件行并计算行数.对于~470 MB的文件(~3.6M行),我得到以下时间(最好的10个):

D次:

real    0m19.146s
user    0m18.932s
sys     0m0.190s
Run Code Online (Sandbox Code Playgroud)

Python时间(在EDIT 2之后,见下文):

real    0m0.924s
user    0m0.792s
sys     0m0.129s
Run Code Online (Sandbox Code Playgroud)

这是D版本,编译有dmd -O -release -inline -m64:

import std.stdio;
import std.string;

int main(string[] args)
{
  if (args.length < 2) {
    return 1;
  }
  auto infile = File(args[1]);
  uint linect = 0;
  foreach (line; infile.byLine())
    linect += 1;
  writeln("There are: ", linect, " lines.");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在相应的Python版本:

import sys

if __name__ == "__main__":
    if (len(sys.argv) < 2): …
Run Code Online (Sandbox Code Playgroud)

python io d

12
推荐指数
1
解决办法
842
查看次数

试图了解Haskell中的函数应用程序运算符

我试图$在Haskell中围绕函数应用程序operator().

我正在研究Learn You a Haskell中的示例,我认为我理解了以下示例:

Prelude> map ($ 3) [(+4), (*10), (^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772] 
Run Code Online (Sandbox Code Playgroud)

然后我尝试了以下变体,它也运行良好:

Prelude> map ($ 3) [(+4), (*10), (\x -> x^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772]
Run Code Online (Sandbox Code Playgroud)

最后,我尝试按如下方式修改列表中的第三个函数,这会生成错误:

Prelude> map ($ 3) [(+4), (*10), (\x -> 2^x), sqrt] 
<interactive>:53:38:
    Ambiguous type variable `b0' in the constraints:
      (Floating b0)
        arising from a use of `sqrt' at <interactive>:53:38-41
      (Integral b0) arising from a use of `^' at <interactive>:53:33
      (Num b0) arising from the literal `3' at <interactive>:53:8
    Probable fix: add a …
Run Code Online (Sandbox Code Playgroud)

haskell operators

10
推荐指数
1
解决办法
433
查看次数

检测脚本中是否从命令行执行脚本?

我是Racket(和Lisp的一般)的新手,我想知道是否有一种规范的方法可以检测脚本是否从命令行运行?

例如,在Python中,执行此操作的标准方法是if __name__ == __main__::

def foo():
    "foo!"

if __name__ == "__main__":
    foo()
Run Code Online (Sandbox Code Playgroud)

现在,假设我有以下的Racket代码,并且respond只有当它作为脚本运行时我才会被调用.

#lang racket
(require racket/cmdline)

(define hello? (make-parameter #f))
(define goodbye? (make-parameter #f))

(command-line #:program "cmdtest"
              #:once-each
              [("-H" "--hello") "Add Hello Message" (hello? #t)]
              [("-G" "--goodbye") "Add goodbye Message" (goodbye? #t)])

(define (respond)
  (printf "~a\n"
          (apply string-append 
                 (cond
                  [(and (hello?) (goodbye?)) '("Hello" " and goodbye.")]
                  [(and (hello?) (not (goodbye?))) '("Hello." "")]
                  [(and (not (hello?)) (goodbye?)) '("" "Goodbye.")]
                  [else '("" "")]))))
Run Code Online (Sandbox Code Playgroud)

有没有简单/标准的方法来实现我想要的?

scheme command-line-interface racket

9
推荐指数
1
解决办法
128
查看次数

标签 统计

command-line-interface ×1

d ×1

haskell ×1

io ×1

operators ×1

python ×1

racket ×1

scheme ×1