2022年1月17日月曜日

The combination of V language and WebAssembly (WASM) is superior to React. It is a state-of-the-art way of thinking. As of January 2022.

https://blog.kyubuns.dev/entry/2020/07/12/150546

I shared it.


Compiling is explosive, no null, Immutable variables by default , Pure functions by default and V language with lots of features you like .

Such V language supports js and wasm output! !! !! 
However, the output method is not yet in the document . 
Well I will get on soon.

I will introduce the build method based on the information collected from the discord of vlang.

Sample code

We will compile the code that enumerates the official Fibonacci sequence.

fibonacci.v

fn fib (a int, b int) {
    val: = a + b
    println (val)
    if val <1000 {
        fib (b, val)
    }
}

fn main () {
    areas: = ['game','web','tools','science','systems','embedded','drivers','GUI','mobile']
    for area in areas {
        println ('Hello, $ area developers!')
    }
    fib (0, 1)
}

Try to run normally

v fibonacci.v

Compiling is about this speed. (Hereafter, all are tried at MBP2018.)
0.25s user 0.04s system 97% cpu 0.301 total

./fibonacci

Execution is about this speed.
0.00s user 0.00s system 56% cpu 0.004 total

js

v -b js -w -o fibonacci fibonacci.v

This will generate fibonacci.js. 
0.00s user 0.00s system 73% cpu 0.008 total It's stupid.

> node fibonacci.js
Hello, game developers!
Hello, web developers!
Hello, tools developers!
Hello, science developers!
Hello, systems developers!
Hello, embedded developers!
Hello, drivers developers!
Hello, GUI developers!
Hello, mobile developers!
1
2
3
Five
8
13
twenty one
34
55 55
89
144
233
377
610
987
1597

0.04s user 0.01s system 91% cpu 0.060 total
To test the execution speed, the number of trials is not enough by about 2 digits, so it is for reference only. 
But isn't this faster than building and running as it is with v? ??

The converted code looks like this.

(There are about 100 lines of builtin definition before )

/ ** @namespace main * / 
const main =  ( function  ( )  { 
    / **
     * @function
     * @param {number} a
     * @param {number} b
     * @returns {void}
    * / 
    function  fib ( a , b )  { 
        / ** @type {number} * 
        / const val = a + b ; 
        builtin .println ( val ) ; if ( val < 1000 ) { fib ( b , val ) ; }
           
            
        

    }

    / * program entry point * / 
    ( function ( )  { 
        / ** @type {string []} * / 
        const areas =  [ "game" ,  "web" ,  "tools" ,  "science" ,  "systems" ,  "embedded" " ,  " drivers " ,  " GUI " ,  " mobile " ] ; 
        for  ( let _tmp1 =  0 ; _tmp1 < areas .length ; " ++ _tmp1 )  { 
            const area= areas [ _tmp1 ] ; 
            builtin .println ( ` Hello, $ { area } developers ! ` ) ; }
        

        fib ( 0 ,  1 ) ; 
    } ) ( ) ;

    / * module exports * / 
    return  { } ; 
} ) ( ) ;

You can see that it is converted very honestly.

wasm

v -o fibonacci.c fibonacci.v
emcc fibonacci.c -s WASM = 1 -o hello.html

No, v is just converted to c! 
Well, that being said, v-> c is as fast as js, but 
c-> wasm takes  2.15s user 0.72s system 224% cpu 1.277 total about a while.

This fibonacci.c swells to a whopping 9559 lines. 

Converting to C is like when you want execution speed instead of compilation speed. 

0 コメント:

コメントを投稿