Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webassembly 简单例子 #9

Open
BUPTlhuanyu opened this issue Feb 26, 2023 · 0 comments
Open

webassembly 简单例子 #9

BUPTlhuanyu opened this issue Feb 26, 2023 · 0 comments

Comments

@BUPTlhuanyu
Copy link
Owner

BUPTlhuanyu commented Feb 26, 2023

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // 1. c++ 代码
        // int fib (int x) {
        //     if (x <= 0) {
        //         return 0;
        //     }
        //     if (x <= 2) {
        //         return 1;
        //     }
        //     return fib(x - 1) + fib(x - 2);
        // }
        // 2. 输出到 https://mbebenita.github.io/WasmExplorer/ 生成 wat 并下载
        // 3. 引入并执行
        fetch('./test.wasm').then((res) => res.arrayBuffer())
            .then((bytes) => WebAssembly.compile(bytes))
            .then((mod) => {
                const ins = new WebAssembly.Instance(mod);
                const a = ins.exports;
                console.time('lib');
                const res = a._Z3fibi(40);
                console.timeEnd('lib');
                console.log(res);
            })

        // js 版本
        // function fib(x) {
        //     if (x <= 0) {
        //         return 0;
        //     }
        //     if (x <= 2) {
        //         return 1;
        //     }
        //     return fib(x - 1) + fib(x - 2);
        // }
        // console.time('lib');
        // const res = fib(40);
        // console.timeEnd('lib');
        // console.log(res);
    </script>
</head>
<body>
    
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant