Code Monkey home page Code Monkey logo

lilydjwg / swapview-rosetta Goto Github PK

View Code? Open in Web Editor NEW
491.0 32.0 107.0 601 KB

Print swap usage per process. Implemented in various programming languages

Shell 5.55% Makefile 9.25% C++ 15.53% C 4.35% C# 2.22% Scheme 8.16% CoffeeScript 4.01% Common Lisp 5.48% D 5.24% Elixir 1.85% Erlang 2.98% Pascal 3.82% Go 4.77% Haskell 5.79% Java 2.18% Julia 1.38% Lua 2.19% Nim 2.12% JavaScript 7.50% OCaml 5.62%
linux programming-language programming-exercise comparison helloworld rust

swapview-rosetta's Introduction

swapview is a simple program to view processes' swap usage on Linux. This repository is the one of multiple implementations, intended to be a lilydjwg's version of Hello World program. For a daily use version, see swapview.

How to run the speed comparison

Install all the dependencies for your interested versions, then run make -k. It will build all that can be built.

To test and see the speed comparison, either use bench.py or run_benchmark. bench.py uses Python 3.4+ and depends on python-toml.

run_benchmark is a Rust version. Install latest Rust and Cargo, then change your working directory to benchmark and run cargo build --release. If the build fails, it's because your Rust is too old or too new....

Run ./run_benchmark <benchmark.toml and wait for it to finish. Failed ones (e.g. because you don't have the dependencies installed) will be marked as failed so you don't need to edit benchmark.toml to disable the ones you can't run.

You can give ./run_benchmark names to selectively run some versions, e.g.

./run_benchmark C C++14 'Rust*' <benchmark.toml

Implementions and their requirements

Of course you should have corresponding compilers / interpreters installed. You can (edit and then) run make to build all that needs to be built.

  • Bash, requires bc
  • Bash_parallel, Bash version using GNU parallel
  • C
  • C++98
  • C++98_omp, openmp paralleled version
  • C++14
  • C++14_boost, C++ version using the boost library
  • C++17
  • CSharp (mono)
  • ChezScheme
  • Chicken, format (will be installed by make)
  • CommonLisp_opt, sbcl
  • CommonLisp_old, sbcl, maynbe others also work
  • CoffeeScript, requires promise (will be installed by make)
  • CoffeeScript_parallel, a parallel version, requires promise (will be installed by make)
  • Crystal, with fiber and multi-process versions, tested with 0.28.0
  • Cython
  • D, dmd or ldmd (LLVM version)
  • Dart
  • Elixir
  • Erlang
  • Go >=1.8
  • Guile >= 2.1
  • Haskell, requires haskell-strict
  • Java, >= Java 8
  • Lua, requires lua-filesystem, works with 5.1, 5.2, 5.3 and LuaJIT
  • newlisp (tested with 10.7.5)
  • Nim (tested with 0.17.0)
  • NodeJS, requires sprintf (will be installed by make)
  • NodeJS_async, another NodeJS version which use async I/O, requires sprintf and async (will be installed by make)
  • NodeJS_cluster, uses multiple forked worker process via Node Cluster API
  • OCaml
  • FreePascal
  • Perl
  • Python, works with Python 2 & 3
  • Python3_bytes
  • R
  • Racket
  • Ruby and Rubinius
  • Rust, >= 1.20
  • Scala
  • Vala, requires glib2 (libglib-2.0 and libgio-2.0)
  • Tcl, >= 8.6
  • Zig, tested with 0.9.1

Contributions

Contributions are welcome! Improve current ones, or submit new one. But make sure your implementations meet the following requirements:

  1. Must be readable and maintainable
  2. Output exact the same format as other versions (but sorting may be unstable)
  3. Try to be efficient
  4. Please include a Makefile if appropriate
  5. Don't forget to tell the compiler to optimize

My Result

This is tested on Arch Linux, with latest versions of compilers and interpreters available there at testing time. CPU is Intel(R) Core(TM) i7-7700HQ.

Updated at 2020-06-13:

           Rust_parallel: top:   31.48, min:   29.66, avg:   32.83, max:   36.90, mdev:    1.86, cnt:  20
               C++98_omp: top:   49.14, min:   45.32, avg:   53.43, max:   72.15, mdev:    6.00, cnt:  20
         Crystal_process: top:   65.50, min:   64.67, avg:   69.53, max:   90.86, mdev:    6.32, cnt:  20
            Go_goroutine: top:   96.19, min:   80.59, avg:  107.33, max:  152.03, mdev:   15.97, cnt:  20
                    Rust: top:  105.02, min:  104.25, avg:  106.21, max:  109.86, mdev:    1.64, cnt:  20
                   C++98: top:  142.99, min:  141.01, avg:  145.01, max:  151.68, mdev:    2.62, cnt:  20
                   C++17: top:  143.39, min:  141.34, avg:  145.20, max:  149.27, mdev:    2.15, cnt:  20
             C++14_boost: top:  144.02, min:  142.48, avg:  147.36, max:  161.02, mdev:    4.98, cnt:  20
                   C++14: top:  144.63, min:  142.52, avg:  146.46, max:  151.17, mdev:    2.27, cnt:  20
                       C: top:  144.65, min:  141.69, avg:  152.78, max:  191.49, mdev:   11.96, cnt:  20
                     PHP: top:  145.38, min:  143.82, avg:  147.61, max:  156.60, mdev:    3.28, cnt:  20
              Python3_mp: top:  157.63, min:  152.96, avg:  160.75, max:  167.09, mdev:    3.74, cnt:  20
                   C++11: top:  171.00, min:  168.77, avg:  172.47, max:  181.06, mdev:    2.43, cnt:  20
                  Cython: top:  181.13, min:  179.10, avg:  183.45, max:  192.59, mdev:    2.98, cnt:  20
                      Go: top:  193.06, min:  191.68, avg:  195.08, max:  201.55, mdev:    2.64, cnt:  20
                    Vala: top:  196.43, min:  193.41, avg:  200.22, max:  212.19, mdev:    4.70, cnt:  20
         D_parallel_llvm: top:  196.72, min:  191.97, avg:  200.57, max:  213.42, mdev:    5.15, cnt:  20
              D_parallel: top:  205.46, min:  199.73, avg:  208.67, max:  216.93, mdev:    4.29, cnt:  20
                 Crystal: top:  214.76, min:  210.48, avg:  217.29, max:  224.11, mdev:    3.25, cnt:  20
           Crystal_fiber: top:  226.87, min:  225.04, avg:  233.10, max:  272.75, mdev:   12.50, cnt:  20
                  LuaJIT: top:  226.97, min:  224.28, avg:  231.18, max:  240.60, mdev:    4.86, cnt:  20
        Haskell_parallel: top:  227.06, min:  216.14, avg:  235.88, max:  260.16, mdev:   11.77, cnt:  20
                   OCaml: top:  230.78, min:  229.04, avg:  233.81, max:  242.57, mdev:    3.83, cnt:  20
                 Python2: top:  232.30, min:  228.44, avg:  235.14, max:  245.36, mdev:    3.79, cnt:  20
                  NodeJS: top:  235.54, min:  233.22, avg:  239.51, max:  254.48, mdev:    5.11, cnt:  20
                    PyPy: top:  247.96, min:  244.07, avg:  253.04, max:  285.93, mdev:    8.61, cnt:  20
                  D_llvm: top:  249.79, min:  247.05, avg:  259.87, max:  312.30, mdev:   17.22, cnt:  20
                 Python3: top:  255.07, min:  254.20, avg:  260.64, max:  281.31, mdev:    7.91, cnt:  20
           Python3_bytes: top:  257.99, min:  252.36, avg:  265.09, max:  289.54, mdev:   10.23, cnt:  20
                       D: top:  267.79, min:  266.07, avg:  272.18, max:  301.44, mdev:    7.70, cnt:  20
                     Nim: top:  279.93, min:  278.35, avg:  286.19, max:  314.88, mdev:    8.93, cnt:  20
                    Ruby: top:  281.40, min:  278.51, avg:  285.42, max:  296.61, mdev:    5.06, cnt:  20
                   Lua51: top:  286.97, min:  282.82, avg:  292.06, max:  316.65, mdev:    7.19, cnt:  20
                  Erlang: top:  290.70, min:  219.62, avg:  329.77, max:  393.73, mdev:   51.76, cnt:  20
                   Lua52: top:  294.91, min:  290.32, avg:  300.19, max:  322.82, mdev:    7.16, cnt:  20
                    Perl: top:  300.15, min:  296.57, avg:  307.49, max:  323.94, mdev:    8.76, cnt:  20
                   Lua53: top:  309.14, min:  304.81, avg:  314.05, max:  332.84, mdev:    6.66, cnt:  20
                 Haskell: top:  326.48, min:  323.48, avg:  331.62, max:  346.78, mdev:    6.34, cnt:  20
                 Chicken: top:  376.63, min:  370.26, avg:  382.90, max:  426.14, mdev:   11.16, cnt:  20
             PyPy3_bytes: top:  409.92, min:  398.71, avg:  422.03, max:  468.93, mdev:   17.08, cnt:  20
              FreePascal: top:  417.88, min:  415.39, avg:  423.13, max:  453.09, mdev:    8.20, cnt:  20
          NodeJS_cluster: top:  422.85, min:  409.16, avg:  430.56, max:  444.07, mdev:    9.24, cnt:  20
            CoffeeScript: top:  440.48, min:  420.74, avg:  469.86, max:  555.34, mdev:   38.53, cnt:  20
              ChezScheme: top:  454.38, min:  451.57, avg:  459.67, max:  471.25, mdev:    6.15, cnt:  20
                   PyPy3: top:  456.18, min:  449.58, avg:  474.78, max:  521.61, mdev:   22.42, cnt:  20
                    Java: top:  462.47, min:  455.63, avg:  468.49, max:  478.00, mdev:    6.90, cnt:  20
                   Guile: top:  489.58, min:  478.95, avg:  499.54, max:  529.63, mdev:   13.28, cnt:  20
   CoffeeScript_parallel: top:  500.05, min:  481.84, avg:  524.93, max:  588.15, mdev:   30.29, cnt:  20
            NodeJS_async: top:  518.14, min:  484.02, avg:  548.72, max:  619.11, mdev:   36.99, cnt:  20
                     Tcl: top:  519.87, min:  504.80, avg:  529.65, max:  553.50, mdev:   12.06, cnt:  20
             Dart_native: top:  622.26, min:  616.78, avg:  634.25, max:  675.66, mdev:   16.27, cnt:  20
          CommonLisp_opt: top:  643.17, min:  638.69, avg:  654.79, max:  697.46, mdev:   14.71, cnt:  20
          CommonLisp_old: top:  678.22, min:  669.22, avg:  688.15, max:  701.31, mdev:   11.21, cnt:  20
                   Julia: top:  709.08, min:  702.52, avg:  721.52, max:  753.88, mdev:   15.30, cnt:  20
         Racket_compiled: top:  807.49, min:  799.71, avg:  820.90, max:  852.44, mdev:   15.17, cnt:  20
                  Racket: top:  809.93, min:  802.91, avg:  837.35, max:  970.74, mdev:   46.79, cnt:  20
                  CSharp: top:  828.85, min:  819.36, avg:  843.39, max:  905.66, mdev:   19.79, cnt:  20
                    Dart: top:  843.20, min:  829.84, avg:  861.74, max:  949.99, mdev:   27.52, cnt:  20
                   Scala: top:  944.85, min:  897.81, avg:  996.39, max: 1122.12, mdev: 4186.68, cnt:  20
                  Elixir: top: 1072.99, min: 1054.52, avg: 1096.13, max: 1242.46, mdev: 4186.40, cnt:  20
           Bash_parallel: top: 1163.78, min: 1133.18, avg: 1219.09, max: 1340.50, mdev: 4186.69, cnt:  20
              POSIX_dash: top: 1393.27, min: 1370.19, avg: 1412.92, max: 1461.04, mdev: 4074.64, cnt:  20
                       R: top: 1475.58, min: 1458.35, avg: 1498.45, max: 1583.44, mdev: 4074.68, cnt:  20
              POSIX_bash: top: 1563.31, min: 1552.28, avg: 1574.53, max: 1605.96, mdev: 4074.59, cnt:  20
               POSIX_zsh: top: 1593.08, min: 1578.51, avg: 1620.55, max: 1719.31, mdev: 4062.81, cnt:  19
                    Bash: top: 2350.38, min: 2223.25, avg: 2506.19, max: 2867.53, mdev: 3579.59, cnt:  13
              PowerShell: top: 3695.72, min: 3680.86, avg: 3755.14, max: 3893.89, mdev: 2148.64, cnt:   8
               OCaml_lwt: FAILED with No such file or directory (os error 2)
           Ruby_rubinius: FAILED with No such file or directory (os error 2)

Java is Oracle JDK. Ruby_rubinius is not included because no packages available for Arch Linux. OCaml_lwt one is broken at the time of running the benchmark.

swapview-rosetta's People

Contributors

15532 avatar 71e6fd52 avatar antigen-1 avatar artoria2e5 avatar cjex avatar coolwanglu avatar dantmnf avatar dhcmrlchtdj avatar ekd123 avatar evincemoi avatar falood avatar farseerfc avatar felixonmars avatar frantic1048 avatar fwolf avatar hexchain avatar hmgle avatar ipfans avatar jahiy avatar lilydjwg avatar longlene avatar quininer avatar sjamaan avatar swordfeng avatar tiye avatar wakeofwind avatar xuanwo avatar yshui avatar yuyichao avatar zhangkaizhao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swapview-rosetta's Issues

About the project

I personally think this evaluation itself does not make much sense, when a joke. If you wish, you can add more programming languages
list:

  1. Tiobe
  2. PYPL
  3. StackOverflow
  4. Github
  5. HackerEarth
  6. indeed
  7. Glassdoor

specific:

  1. aauto
  2. EPL
  3. ActionScript
  4. lazarus
  5. Clojure
  6. vba
    and other
    The language is infinite, the future, the programming language will become more and more differentiation, play a role in their own places so I think JavaScript to do with the full stack is not wise.

c++17: need parallel <execution> example

Many of the loops will have to be rewritten as std::transform, for_each, and reduce lambda operations.


Before we start using the c++17 ts, we can test libstdc++ STL parallelism with -fopenmp -D_GLIBCXX_PARALLEL. Almost no change to the code is needed, but as far as I can see the only improvement would be on sort() which is pointless.


Possible implementation:

#if defined(PARALLEL)
#include <execution>
// Writing the currying thing takes a lot of time:
// http://cpptruths.blogspot.com/2018/12/simple-template-currying.html
#define sort(...) std::sort(std::execution::par_unseq, __VA_ARGS__)
#define transform(...) std::transform(std::execution::par_unseq, __VA_ARGS__)
#define remove_if(...)  std::remove_if(std::execution::par_unseq, __VA_ARGS__)
#else
using std::sort;
using std::transform;
using std::remove_if;
#endif

/* ... */

auto get_swap() -> std::vector<swap_info> {
  std::vector<swap_info> result;
  auto proc = std::filesystem::directory_iterator{"/proc"};
  transform(proc.begin(), proc.end(), result.begin(), [](const auto& entry) {
    if (int pid = std::strtol(entry.path().filename().c_str(), nullptr, 10))
      if (size_t swp = get_swap_for(entry.path() / "smaps"))
        return {pid, swp, get_comm_for(entry.path() / "cmdline")};
    return {0, 0.0, ""};
  });
  result.erase(remove_if(result.begin(), result.end(), [](const auto& maybe_res) {
    return std::get<0>(maybe_res) == 0;
  }));
  sort(result.begin(), result.end(), [](const auto &lhs, const auto &rhs) {
    return std::get<1>(lhs) < std::get<1>(rhs);
  }); 
  return result;
}

代码没可比性

这个测试demo代码本身就没啥可比性,语言解析损耗简直可以忽略,大部分瓶颈都在系统io上,像printf read最后还是调用了底层本地代码。。如果纯算法写个pi什么的 去对比。。排名肯定大改 = =

[bug] Go_goroutine 版本 goroutine 同步问题

GetInfos() 函数返回前并没有等待 info_ch 里面的数据处理完:

	go func(info_ch chan *Info, list *[]Info) {
		for {
			tmp := <-info_ch
			if tmp != nil {
				*list = append(*list, *tmp)
			}
		}
	}(info_ch, &list)

我运行了该程序几次,大部分时候的结果都是漏加了数据。把上面的代码 sleep 一下,就 100% 重现了:

	go func(info_ch chan *Info, list *[]Info) {
		for {
			tmp := <-info_ch
			time.Sleep(time.Millisecond * 10)
			if tmp != nil {
				*list = append(*list, *tmp)
			}
		}
	}(info_ch, &list)

正确的做法要保证这个 goroutine 处理完数据该函数才返回。

How to set environment variables in the TOML file?

JIT compilation mode of the Racket's CS implementation can significantly improve the performance of the Racket_parallel version. But it can be enabled only by setting the PLT_CS_JIT environment variable on startup.

Certainly, I can set these variables when starting run_benchmark or python3 bench.py. But I still wonder whether or not I can set them in the configuration file.

example with CoffeeScript and Promise

fs = require 'fs'
Promise = require 'promise'

readdir = Promise.denodeify fs.readdir
read = Promise.denodeify fs.readFile

addString = (sum, line) -> sum + (Number (line.match /\d+/)[0])
addSecond = (sum, aaa) -> sum + aaa[1]

formatSize0 = (n) ->
  formatSize = (acc, n) ->
    if (n > 1100) and (acc <= 3)
    then formatSize (acc + 1), (n /= 1024)
    else
      unit = 'KMGT'[acc]
      num = n.toFixed(1)
      "#{num}#{unit}iB"
  formatSize 0, n

fillLength = (n, str) ->
  if str.length < n
  then fillLength n, " #{str}"
  else str

readdir('/proc')
.then (files) ->
  list = files
  .filter (file) -> file.match /\d+/
  .map (file) ->
    (read "/proc/#{file}/smaps", 'utf8')
    .then (text) ->
      text.split('\n').filter (line) -> line.match(/^Swap/)
    .then (lines) ->
      (read "/proc/#{file}/cmdline", 'utf8')
      .then (text) ->
        cmdline = text.replace /\0/g, ' '
        [file, lines, cmdline]
  (Promise.all list)
  .then (res) ->
    out = res
    .filter (aaa) -> aaa[1].length > 0
    .map (aaa) ->
      aaa[1] = aaa[1].reduce(addString, 0)
      aaa
    .sort (aaa, bbb) -> aaa[1] - bbb[1]
    output out
.then null, (error) ->
  console.error 'error', error

output = (res) ->
  console.log [
    fillLength 5, 'PID'
    fillLength 9, 'SWAP'
    'COMMAND'
  ].join(' ')

  str = res
  .map (aaa) ->
    [
      fillLength 5, aaa[0]
      fillLength 9, (formatSize0 aaa[1])
      aaa[2]
    ].join(' ')
  .join('\n')
  console.log str

  console.log [
    'Total:'
    formatSize0 (res.reduce addSecond, 0)
  ].join('')
  # fs.writeFileSync 'data.json', JSON.stringify(res, null, 2)

Stable releases?

The project has no version and/or release now, which makes it hard to be packaged and published in package manager.
Using git commit hash as version is not a good idea.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.