2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CaLang初回テスト

Posted at

Calang初回テスト

初めましてCaLang_Labのじゅぴです
計算特化型言語CaLangの第一回目のテストです

テスト内容

python ・node.js・C#との計算速度の比較
(PythonでNumpyに大差で負けたためnode.js以降は打ち切り)
計算速度を計測するにあたり以下の計算を行います
・フィボナッチ数列の再帰的計算(n=40)
・巨大素数の計算(10万桁)

フィボナッチ数列の再帰的計算

CaLangでは計算速度の測定は実装していないのでpythonでsubprocessをつかいコマンドラインを操作し、時間を測定します。

import time
import subprocess

def run_command_and_measure_time(command):
    start_time = time.time()
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    end_time = time.time()
    elapsed_time = end_time - start_time
    print(f"Command Output:\n{result.stdout}")
    print(f"Command Error (if any):\n{result.stderr}")
    print(f"Time taken: {elapsed_time:.10f} seconds")

if __name__ == "__main__":
    command = "D:\\mast\\lexer.exe main.ca"
    run_command_and_measure_time(command)

PS D:\mast> python3 main.py
Command Output:
Result: 102334155

Command Error (if any):

Time taken: 0.5435197353 seconds

結果は0.5435197353秒でした
続いてPythonやそのほかの言語でも計測します

pythonでのフィボナッチ数列の再帰的計算

import time

def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)

if __name__ == "__main__":
    n = 40 
    start_time = time.time()
    result = fib(n)
    end_time = time.time()
    elapsed_time = end_time - start_time
    print(f"fib({n}) = {result}")
    print(f"Time taken: {elapsed_time:.10f} seconds")

PS D:\mast> python3 main.py
fib(40) = 102334155
Time taken: 12.9329898357 seconds

結果は12.9329898357とかなり遅かったです。
Numpyなどのモジュールを使って検証してみます

import time
import numpy as np
def fib(n):
    if n <= 1:
        return n
    memo = np.zeros(n + 1, dtype=int)
    memo[0], memo[1] = 0, 1
    for i in range(2, n + 1):
        memo[i] = memo[i - 1] + memo[i - 2]
    return memo[n]
if __name__ == "__main__":
    n = 40
    start_time = time.time()
    result = fib(n)
    end_time = time.time()
    elapsed_time = end_time - start_time
    print(f"fib({n}) = {result}")
    print(f"Time taken: {elapsed_time:.10f} seconds")
PS D:\mast> python3 main.py
fib(40) = 102334155
Time taken: 0.0000257492 seconds

0.0000257492秒....
CaLangは0.5435197353秒だったのでNumpyに負けてしまいました。
次回のテストまでにNumpyの計算速度に近づけたいと思います。
以上第一回テストでした!
プレリリースα0.0.1は3/17にリリース予定なのでお楽しみに

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?