From: anton@mips.complang.tuwien.ac.at   
      
   I explored the performance of threads and futures (aka async/await, a   
   variant of cooperative multitasking) in Rust, and as an example   
   program, I have one thread/future that generates numbers from 0 to "   
   if the result is eventually 2DROPped), so it's hard to tell how Rust   
   would perform with buffered I/O (Doing buffered I/O is doable in Rust,   
   but takes a little more work than I want to do at the moment). One   
   problem is that it has to convert the number to a string, and it has   
   to heap-allocate (and later free) that string. In my program, the   
   concatenation of the strings is also a costly operation.   
      
   Here are the programs:   
      
   Forth:   
   ---------------------------------------   
   70 constant linelength   
      
   \ synonym type 2drop   
   \ synonym cr noop   
   \ synonym space noop   
      
   : main ( u1 -- )   
    dup 0= if exit then   
    ." 0"   
    1 swap 1 ?do ( len )   
    i 0 <# #s #> rot 2dup + 1+ linelength > if ( c-addr u len )   
    if ( c-addr u )   
    cr then   
    tuck   
    else   
    space 1+ over + -rot then   
    type loop   
    if cr then ;   
   ---------------------------------------------   
      
   Rust:   
   --------------------------------------------   
   use std::env;   
      
   fn main() {   
    let linelength = 2;   
    let args: Vec = env::args().collect();   
    let arg1 = args.get(1).expect("Usage: ").par   
   e::().unwrap();   
    if arg1>0 {   
    let mut line = "0".to_string();   
    for i in 1..arg1 {   
    let received = i.to_string();   
    if line.len()+1+received.len() > linelength {   
    if line.len()>0 {   
    println!("{line}");   
    }   
    line = received;   
    } else {   
    line = format!("{line} {received}");   
    }   
    }   
    if line.len() > 0 {   
    println!("{line}");   
    }   
    }   
   }   
   -----------------------------------------   
      
   - anton   
   --   
   M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html   
   comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html   
    New standard: https://forth-standard.org/   
   EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html   
   EuroForth 2025 registration: https://euro.theforth.net/   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|