mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-15 08:36:34 +00:00
Merge pull request #91 from achanda/contains-bench
Track benchmark for contains
This commit is contained in:
@ -5,6 +5,8 @@ extern crate ipnetwork;
|
||||
use ipnetwork::{Ipv4Network, Ipv6Network};
|
||||
use criterion::Criterion;
|
||||
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
fn parse_ipv4_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("parse ipv4", |b| b.iter(|| {
|
||||
"127.1.0.0/24".parse::<Ipv4Network>().unwrap()
|
||||
@ -17,5 +19,19 @@ fn parse_ipv6_benchmark(c: &mut Criterion) {
|
||||
}));
|
||||
}
|
||||
|
||||
criterion_group!(benches, parse_ipv4_benchmark, parse_ipv6_benchmark);
|
||||
fn contains_ipv4_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("contains ipv4", |b| b.iter(|| {
|
||||
let cidr = "74.125.227.0/25".parse::<Ipv4Network>().unwrap();
|
||||
cidr.contains(Ipv4Addr::new(74, 125, 227, 4))
|
||||
}));
|
||||
}
|
||||
|
||||
fn contains_ipv6_benchmark(c: &mut Criterion) {
|
||||
c.bench_function("contains ipv6", |b| b.iter(|| {
|
||||
let cidr = "FF01:0:0:17:0:0:0:2/65".parse::<Ipv6Network>().unwrap();
|
||||
cidr.contains(Ipv6Addr::new(0xff01, 0, 0, 0x17, 0x7fff, 0, 0, 0x2))
|
||||
}));
|
||||
}
|
||||
|
||||
criterion_group!(benches, parse_ipv4_benchmark, parse_ipv6_benchmark, contains_ipv4_benchmark, contains_ipv6_benchmark);
|
||||
criterion_main!(benches);
|
||||
|
@ -131,8 +131,8 @@ impl Ipv4Network {
|
||||
/// assert!(!net.contains(Ipv4Addr::new(127, 0, 1, 70)));
|
||||
/// ```
|
||||
pub fn contains(&self, ip: Ipv4Addr) -> bool {
|
||||
let net = u32::from(self.network());
|
||||
let mask = u32::from(self.mask());
|
||||
let mask = !(0xffff_ffff as u64 >> self.prefix) as u32;
|
||||
let net = u32::from(self.addr) & mask;
|
||||
(u32::from(ip) & mask) == net
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user