Giter VIP home page Giter VIP logo

script-bench-rs's People

Contributors

khvzak 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

Watchers

 avatar  avatar  avatar  avatar

script-bench-rs's Issues

Rune script

Rune script example

rune.rs

use std::sync::Arc;
use rune::runtime::GuardedArgs;
use rune::{Context, Diagnostics, Source, Sources, Value, Vm};
use rune::termcolor::{ColorChoice, StandardStream};
use rune::{Any, ContextError, Module};

use criterion::{criterion_group, criterion_main, Criterion};

#[derive(Any, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct RustData(Arc<String>);

impl RustData {
    pub fn new(s: String) -> Self {
        RustData(Arc::new(s))
    }
}

const PROG: &'static str = r#"
pub fn main() {
    let charset = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
    let generate_string = |len| {
        let data = Vec::new();
        for i in 0..len {
            data.push( charset[ rand(charset.len()) ] );
        }
        return concat(data);
    };

    let array = Vec::new();
    generate_string(rand(16) + 1);
    array.push(RustData::new(generate_string(rand(16) + 1)));
    for i in 0..100000 {
        array.push(RustData::new(generate_string(rand(16) + 1)));
    }

    sort( array );
}"#;



pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::new();
    module.function( &["RustData", "new"], RustData::new)?;
    module.ty::<RustData>()?;
    module.function(&["rand"], |n: i64| rand::random::<u32>() as i64 % n )?;
    module.function( &["concat"], |items:Vec<String>| -> String {
        items
            .into_iter()
            .fold( String::new(), |mut c,e| {
                c.push_str(e.as_str() );
                c
            } )
    })?;
    module.function( &["sort"], |mut items:Vec<RustData>| {
        items.sort();
    })?;
    Ok(module)
}

pub fn benchmark(c: &mut Criterion) {
    let mut sources = Sources::new();
    sources.insert( Source::with_path( "", PROG, None::<&str> ) );

    let mut context = Context::with_default_modules().unwrap();
    context.install( &module().unwrap() ).unwrap();

    let runtime = Arc::new( context.runtime() );

    let mut diag = Diagnostics::new();

    let result = rune::prepare(&mut sources)
        .with_context(&context)
        .with_diagnostics(&mut diag)
        .build();

    if !diag.is_empty() {
        let mut writer = StandardStream::stderr(ColorChoice::Always);
        diag.emit(&mut writer, &sources).unwrap();
    }

    let unit = result.unwrap();

    let mut vm = Vm::new(runtime, Arc::new(unit));
    c.bench_function("Sort userdata", |b| {
        b.iter(|| {
            let _ = vm.call( &["main"], () ).unwrap();
        });
    });
}

criterion_group! {
    name = benches;
    config = Criterion::default().sample_size(10);
    targets = benchmark,
}

criterion_main!(benches);

Cargo.toml

[dependencies]
rune = { version = "0.12.0", optional = true }

Yep. It's not fair because the sort comparison logic isn't in the script.
But i don't know how fix it right now.

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.