khvzak / script-bench-rs Goto Github PK
View Code? Open in Web Editor NEWRust embedded scripting languages benchmark
Rust embedded scripting languages benchmark
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.
mlua here is obviously the fastest but i wonder how much mem is used compared with the rest and can there be faster embedded rustlang scripting languages as well?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.