История изменений
Исправление fsb4000, (текущая версия) :
Посмотрел твой код, да действительно regex работает быстрее contains, кто бы знал…
Я нашёл одно место, которое вероятно ты уже тоже увидел:
В С версии мы параллелим самый верхний цикл for (после числа попыток)
#define NUM_SEARCHES UINT64_C(1000)
/* ... */
size_t count = 0;
#pragma omp parallel for reduction(+ : count)
for (size_t iter = 0; iter < NUM_SEARCHES; iter++) {
А в Rust нет. Я передвинул into_par_iter() повыше, и стало побыстрее.
Ещё я передвинул создание needle и needle_regex внутрь, потому что в условие вроде так было, и у меня на С…
https://i.postimg.cc/yNGVJHKm/image.png
extern crate rand;
extern crate rayon;
use rand::Rng;
use rand::distributions::Alphanumeric;
use rayon::prelude::*;
use std::time::Instant;
use regex::Regex;
use regex;
fn main() {
for _ in 0..10 {
let timer = Instant::now();
let vec: Vec<String> = (0..100_000)
.into_par_iter()
.map_init(
|| rand::thread_rng(),
|rng, _| rng.sample_iter(&Alphanumeric).take(100).collect()
).collect();
let overall_count: u32 =
(0..1000)
.into_par_iter()
.fold(|| 0u32, |acc, _| {
let needle: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
.collect();
let needle_regex = Regex::new(®ex::escape(&needle)).unwrap();
if (&vec).iter().any(|s100 | {
needle_regex.find(&s100).is_some()
}) {
acc + 1
}
else {
acc
}
})
.sum();
println!("Found {} instances of in {:.2} secs", overall_count,
timer.elapsed().as_secs_f64());
}
}
Исправление fsb4000, :
Посмотрел твой код, да действительно regex работает быстрее contains, кто бы знал…
Я нашёл одно место, которое вероятно ты уже тоже увидел:
В С версии мы параллелим самый верхний цикл for (после числа попыток)
#define NUM_SEARCHES UINT64_C(1000)
/* ... */
size_t count = 0;
#pragma omp parallel for reduction(+ : count)
for (size_t iter = 0; iter < NUM_SEARCHES; iter++) {
А в Rust нет. Я передвинул into_par_iter() повыше, и стало побыстрее.
Ещё я передвинул создание needle и needle_regex внутрь, потому что в условие вроде так было, у меня на С…
https://i.postimg.cc/yNGVJHKm/image.png
extern crate rand;
extern crate rayon;
use rand::Rng;
use rand::distributions::Alphanumeric;
use rayon::prelude::*;
use std::time::Instant;
use regex::Regex;
use regex;
fn main() {
for _ in 0..10 {
let timer = Instant::now();
let vec: Vec<String> = (0..100_000)
.into_par_iter()
.map_init(
|| rand::thread_rng(),
|rng, _| rng.sample_iter(&Alphanumeric).take(100).collect()
).collect();
let overall_count: u32 =
(0..1000)
.into_par_iter()
.fold(|| 0u32, |acc, _| {
let needle: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
.collect();
let needle_regex = Regex::new(®ex::escape(&needle)).unwrap();
if (&vec).iter().any(|s100 | {
needle_regex.find(&s100).is_some()
}) {
acc + 1
}
else {
acc
}
})
.sum();
println!("Found {} instances of in {:.2} secs", overall_count,
timer.elapsed().as_secs_f64());
}
}
Исходная версия fsb4000, :
Посмотрел твой код, да действительно regex работает быстрее contains, кто бы знал…
Я нашёл одно место, которое вероятно ты уже тоже увидел:
В С версии мы параллелим самый верхний (после числа попыток)
#define NUM_SEARCHES UINT64_C(1000)
/* ... */
size_t count = 0;
#pragma omp parallel for reduction(+ : count)
for (size_t iter = 0; iter < NUM_SEARCHES; iter++) {
А в Rust нет. Я передвинул into_par_iter() повыше, и стало побыстрее.
Ещё я передвинул создание needle и needle_regex внутрь, потому что в условие вроде так было, у меня на С…
https://i.postimg.cc/yNGVJHKm/image.png
extern crate rand;
extern crate rayon;
use rand::Rng;
use rand::distributions::Alphanumeric;
use rayon::prelude::*;
use std::time::Instant;
use regex::Regex;
use regex;
fn main() {
for _ in 0..10 {
let timer = Instant::now();
let vec: Vec<String> = (0..100_000)
.into_par_iter()
.map_init(
|| rand::thread_rng(),
|rng, _| rng.sample_iter(&Alphanumeric).take(100).collect()
).collect();
let overall_count: u32 =
(0..1000)
.into_par_iter()
.fold(|| 0u32, |acc, _| {
let needle: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(20)
.collect();
let needle_regex = Regex::new(®ex::escape(&needle)).unwrap();
if (&vec).iter().any(|s100 | {
needle_regex.find(&s100).is_some()
}) {
acc + 1
}
else {
acc
}
})
.sum();
println!("Found {} instances of in {:.2} secs", overall_count,
timer.elapsed().as_secs_f64());
}
}