Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 1.05 KB

rayon-parallel-sort.zh.md

File metadata and controls

29 lines (22 loc) · 1.05 KB

并行,排序 vector

[![rayon-badge]][rayon] [![rand-badge]][rand] [![cat-concurrency-badge]][cat-concurrency]

此示例:并行排序字符串 vector。

分配一个空字符串的 vector。par_iter_mut().for_each会并发填充随机值。虽然存在多种选择,可以对可枚举数据类型进行排序,par_sort_unstable通常比稳定排序算法快。

extern crate rand;
extern crate rayon;

use rand::{Rng, thread_rng};
use rand::distributions::Alphanumeric;
use rayon::prelude::*;

fn main() {
  let mut vec = vec![String::new(); 100_000];
  vec.par_iter_mut().for_each(|p| {
    let mut rng = thread_rng();
    *p = (0..5).map(|_| rng.sample(&Alphanumeric)).collect()
  });
  vec.par_sort_unstable();
}