結論からいうとC++が 2~3秒 のときに jankは 100秒くらい、Pythonもほぼ同程度だった。ということは現状のjankの動作速度はPythonくらいだと思うのが良さそう。-O3 オプションをつけても結果はあまり変わらなかった。

メモリ使用量も測りたかったけど、今回は未計測。 【追記】 メモリ消費量については後述。)

(ns jank-tarai-test.main)
 
(cpp/raw "
 
#include <iostream>
 
std::chrono::high_resolution_clock::time_point now(){
  return std::chrono::high_resolution_clock::now();          
}
 
float diff(
          const std::chrono::high_resolution_clock::time_point& start,
          const std::chrono::high_resolution_clock::time_point& end
          ){
  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  return duration.count() / 1000000.0;
}
 
 
int tarai(int x, int y, int z) {
  if (x <= y) {
    return y;
  }
  return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y));
}
 
")
 
(defn tarai [x y z]
  (do
    (if (<= x y)
      y
      (tarai
       (tarai (- x 1) y z)
       (tarai (- y 1) z x)
       (tarai (- z 1) x y)))))
 
(defn impl_cpp [] 
  (println "result:" (cpp/tarai 15 5 0)))
 
(defn impl_clj [] 
  (println "result:" (tarai 15 5 0)))
 
(defn timeit [func]
  (let [start (cpp/now)]
    (func)
    (let [end (cpp/now)]
      (println "elapsed (sec):" (cpp/diff start end)))))
 
;; (defn -main [& args]
;;   (println "cpp tarai start...")
;;   (timeit impl_cpp)
;;   (println "cpp tarai end")
;; 
;;   (println "clj tarai start...")
;;   (timeit impl_clj)
;;   (println "clj tarai end"))
 
(println "cpp tarai start...")
(timeit impl_cpp)
(println "cpp tarai end")
 
(println "clj tarai start...")
(timeit impl_clj)
(println "clj tarai end")
$ jank run test.jank
cpp tarai start...
result: 15
elapsed (sec): 2.898941
cpp tarai end
clj tarai start...
result: 15
elapsed (sec): 103.875992
clj tarai end
def tarai(x: int, y: int, z: int) -> int:
    if x <= y:
        return y
    return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y))
 
print(tarai(15, 5, 0))
$ time python tarai.py
15

real    1m46.282s
user    0m0.000s
sys     0m0.015s

jankはC++呼べてラッパーも書かずに済んで楽で楽しいーと思ってたけど、現実的には相当C++コードに頼る感じになりそう。ということは結果的にグルー言語としてPythonっぽい使い方になるのかな。

現状Pythonでも相当数のライブラリがバインディング済みなことを考えると、わざわざjankを使うニーズってあんまりないのかも…。Clojure (ClojureScript) のパフォーマンスが相当良いことを考えても、Clojureサイドから見ても現状でわざわざjankを使うメリットはあんまりなさそうで、案外板挟みか…?

そう思うと、C++のライブラリでちょっと遊んだり実験するときの対話環境くらいに捉えるといいのかな。シェルっぽい使い方が相性はよさそう。(babashka的な。)

【追記】 メモリ消費量も測ってみた

結果を先に。jankはC++の2~3倍程度のメモリ消費量に収まっているようでちょっと安心した。

$ jank run main.jank

cpp tarai start...
result: 15
elapsed (sec): 3.340047
memory:  2957312
memoryPeak:  3416064
cpp tarai end

clj tarai start...
result: 15
elapsed (sec): 97.118896
memory:  7442432
memoryPeak:  7028736
clj tarai end

コード:

(ns jank-tarai-test.main)
 
(cpp/raw "
          
// Source - https://stackoverflow.com/a/14927379
// Posted by oblitum, modified by community. See post 'Timeline' for change history
// Retrieved 2026-03-31, License - CC BY-SA 4.0
 
/*
 * Author:  David Robert Nadeau
 * Site:    http://NadeauSoftware.com/
 * License: Creative Commons Attribution 3.0 Unported License
 *          http://creativecommons.org/licenses/by/3.0/deed.en_US
 */
 
#if defined(_WIN32)
#include <windows.h>
#include <psapi.h>
 
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/resource.h>
 
#if defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
 
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
#include <fcntl.h>
#include <procfs.h>
 
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#include <stdio.h>
 
#endif
 
#else
#error \"Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS.\"
#endif
 
 
/**
 * Returns the peak (maximum so far) resident set size (physical
 * memory use) measured in bytes, or zero if the value cannot be
 * determined on this OS.
 */
size_t getPeakRSS( )
{
#if defined(_WIN32)
    /* Windows -------------------------------------------------- */
    PROCESS_MEMORY_COUNTERS info;
    GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
    return (size_t)info.PeakWorkingSetSize;
 
#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
    /* AIX and Solaris ------------------------------------------ */
    struct psinfo psinfo;
    int fd = -1;
    if ( (fd = open( \"/proc/self/psinfo\", O_RDONLY )) == -1 )
        return (size_t)0L;      /* Can't open? */
    if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
    {
        close( fd );
        return (size_t)0L;      /* Can't read? */
    }
    close( fd );
    return (size_t)(psinfo.pr_rssize * 1024L);
 
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
    /* BSD, Linux, and OSX -------------------------------------- */
    struct rusage rusage;
    getrusage( RUSAGE_SELF, &rusage );
#if defined(__APPLE__) && defined(__MACH__)
    return (size_t)rusage.ru_maxrss;
#else
    return (size_t)(rusage.ru_maxrss * 1024L);
#endif
 
#else
    /* Unknown OS ----------------------------------------------- */
    return (size_t)0L;          /* Unsupported. */
#endif
}
 
 
/**
 * Returns the current resident set size (physical memory use) measured
 * in bytes, or zero if the value cannot be determined on this OS.
 */
size_t getCurrentRSS( )
{
#if defined(_WIN32)
    /* Windows -------------------------------------------------- */
    PROCESS_MEMORY_COUNTERS info;
    GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
    return (size_t)info.WorkingSetSize;
 
#elif defined(__APPLE__) && defined(__MACH__)
    /* OSX ------------------------------------------------------ */
    struct mach_task_basic_info info;
    mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
    if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
        (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
        return (size_t)0L;      /* Can't access? */
    return (size_t)info.resident_size;
 
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
    /* Linux ---------------------------------------------------- */
    long rss = 0L;
    FILE* fp = NULL;
    if ( (fp = fopen( \"/proc/self/statm\", \"r\" )) == NULL )
        return (size_t)0L;      /* Can't open? */
    if ( fscanf( fp, \"%*s%ld\", &rss ) != 1 )
    {
        fclose( fp );
        return (size_t)0L;      /* Can't read? */
    }
    fclose( fp );
    return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
 
#else
    /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
    return (size_t)0L;          /* Unsupported. */
#endif
}
 
 
#include <iostream>
 
std::chrono::high_resolution_clock::time_point now(){
  return std::chrono::high_resolution_clock::now();          
}
 
float diff(
          const std::chrono::high_resolution_clock::time_point& start,
          const std::chrono::high_resolution_clock::time_point& end
          ){
  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  return duration.count() / 1000000.0;
}
 
 
int tarai(int x, int y, int z) {
  if (x <= y) {
    return y;
  }
  return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y));
}
 
")
 
(defn tarai [x y z]
  (do
    (if (<= x y)
      y
      (tarai
       (tarai (- x 1) y z)
       (tarai (- y 1) z x)
       (tarai (- z 1) x y)))))
 
(defn impl_cpp [] 
  (println "result:" (cpp/tarai 15 5 0)))
 
(defn impl_clj [] 
  (println "result:" (tarai 15 5 0)))
 
(defn timeit [func]
  (let [start (cpp/now)
        currentMemory1 (cpp/getCurrentRSS)
        currentPeakMemory1 (cpp/getPeakRSS)]
    (func)
    (let [end (cpp/now)
        currentMemory2 (cpp/getCurrentRSS)
        currentPeakMemory2 (cpp/getPeakRSS)]
      (println "elapsed (sec):" (cpp/diff start end))
      (println "memory: " (- currentMemory2 currentMemory1))
      (println "memoryPeak: " (- currentPeakMemory2 currentPeakMemory1)))))
 
;; (defn -main [& args]
;;   (println "cpp tarai start...")
;;   (timeit impl_cpp)
;;   (println "cpp tarai end")
;;   (println "")
;;   (println "clj tarai start...")
;;   (timeit impl_clj)
;;   (println "clj tarai end"))
 
(println "cpp tarai start...")
(timeit impl_cpp)
(println "cpp tarai end")
(println "")
(println "clj tarai start...")
(timeit impl_clj)
(println "clj tarai end")

Python版も一応。ただ、どう比較するべきかよくわからず。

$ python tarai.py

15
time 93.24902269999984

snapshot diff
C:\miniconda3\Lib\tracemalloc.py:560: size=328 B (+328 B), count=1 (+1), average=328 B
C:\miniconda3\Lib\tracemalloc.py:423: size=328 B (+328 B), count=1 (+1), average=328 B

current, peak memory
current: 1600
peak:    2408

memory
pmem(rss=19783680, vms=9900032, num_page_faults=8399, peak_wset=19795968, wset=19783680, peak_paged_pool=147368, paged_pool=147192, peak_nonpaged_pool=12640, nonpaged_pool=12096, pagefile=9900032, peak_pagefile=10010624, private=9900032)
import os
import psutil
import tracemalloc
import time
 
def tarai(x: int, y: int, z: int) -> int:
    if x <= y:
        return y
    return tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y))
 
tracemalloc.start()
snapshot_before = tracemalloc.take_snapshot()
 
start = time.perf_counter()
 
print(tarai(15, 5, 0))
 
end = time.perf_counter()
snapshot_after = tracemalloc.take_snapshot()
 
print("time", end-start)
 
all_stats = snapshot_after.compare_to(snapshot_before, 'lineno')
inc_stats = [stat for stat in all_stats if stat.size_diff > 0]
print("\nsnapshot diff")
for stat in inc_stats[:3]:
    print(stat)
 
cur, peak = tracemalloc.get_traced_memory()
print("\ncurrent, peak memory")
print(f"current: {cur}")
print(f"peak:    {peak}")
 
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
print("\nmemory")
print(mem_info)

ちなみにこの件を discussionsで報告してみたところ、詳しくは分析してみないとわからないけれどJITが効いていないわけではないらしくて、パフォーマンスの最適化を今後数ヶ月かけて行うとのこと。期待して良さそうな返事だったので、半年後が楽しみ。手伝える部分があったら手伝いたいけどな。とりあえずjankやClojureで遊んでいよう。


このエントリーをはてなブックマークに追加follow us in feedly