Warum ist dieser C ++ - Code viel langsamer als Rost?C++

Programme in C++. Entwicklerforum
Guest
 Warum ist dieser C ++ - Code viel langsamer als Rost?

Post by Guest »

Ich habe zwei identische Programme erstellt (eine in Rost und eine in C ++). Sie sind einzelne Gewinde und es gibt keine Haufen Zuweisungen. C ++ - Version (kompiliert mit MSVC /O2 -Release oder GCC) läuft in 17 Sek. Profilgeführte Optimierung verbesserte die Leistung nicht. Core (s), 12 logische Prozessor (s). < /P>

Warum ist die Rost -Version so viel schneller? />
Vielleicht macht Rost eine bessere Schlaufe ab als C ++? p> Was kann getan werden, um die Leistung der C ++ - Version zu verbessern? />

Code: Select all

use std::time::SystemTime;

pub struct BWRNG {
pub seed:u64,
}

impl BWRNG {
const ADD: u64 = 0x269ec3;
const MULT: u64 = 0x5d588b656c078965;

pub fn new(seed:u64) -> BWRNG {
BWRNG { seed }
}
pub fn next(&mut self) -> u64
{
self.seed = self.seed.wrapping_mul(Self::MULT).wrapping_add(Self::ADD);
self.seed
}
pub fn advance(&mut self, advances:u32){
for _ in 0..advances
{
self.next();
}
}
pub fn next_uint2(&mut self, max:u32) -> u32
{
(((self.next() >> 32) * (max as u64)) >> 32) as u32
}
pub fn get_seed(&self) -> u64
{
self.seed
}
pub fn get_current_value(&self) -> u64
{
self.seed
}
pub fn next_uint(&mut self) -> u32
{
(self.next() >> 32) as u32
}

fn advance_probability_table(&mut self) -> u32 {
let mut count:u32 = 0;

count += 1;
self.advance(1);

count += 1;
if self.next_uint2(101) > 50
{
count += 1;
self.advance(1);
}

count += 1;
if self.next_uint2(101) > 30
{
count += 1;
self.advance(1);
}

count += 1;
if self.next_uint2(101) > 25
{
count += 1;
if self.next_uint2(101) > 30
{
count += 1;
self.advance(1);
}
}

count += 1;
if self.next_uint2(101) > 20
{
count += 1;
if self.next_uint2(101) > 25
{
count += 1;
if self.next_uint2(101) > 33
{
count += 1;
self.advance(1);
}
}
}

return count;
}

pub fn initial_advances_bw2(seed:u64) -> u32 {
let mut rng = BWRNG::new(seed);
let mut count:u32 = 0;

for i in 0..5 {
count += rng.advance_probability_table();

if i == 0{
count += 3;
rng.advance(3);
}
}

for _limit in 0..100 {
count += 3;
let rand1 = rng.next_uint2(15);
let rand2 = rng.next_uint2(15);
let rand3 = rng.next_uint2(15);

if rand1 != rand2 && rand1 != rand3 && rand2 != rand3
{
break;
}
}

return count;
}

}

fn main() {
let time_start = SystemTime::now();

let mut result = 0u64;

for i in 0..100_000_000u64 {
result += BWRNG::initial_advances_bw2(i) as u64;
}

let dur = SystemTime::now().duration_since(time_start).unwrap().as_secs();
println!("Dur={}sec.  Result={}", dur, result % 255);
}

< /code>
C ++: < /p>

using u64 = unsigned long long;
using u32 = unsigned int;
using u16 = unsigned short;
using u8 = unsigned char;

class BWRNG
{
public:
BWRNG(u64 seed = 0) : seed(seed)
{
}

void advance(u32 advances)
{
for (u32 advance = 0; advance < advances; advance++)
{
next();
}
}

u64 next()
{
return seed = seed * 0x5d588b656c078965 + 0x269ec3;
}

u32 nextUInt(u32 max)
{
return ((next() >> 32) * max) >> 32;
}

u32 nextUInt()
{
return next() >> 32;
}

u64 getSeed() const
{
return seed;
}

static u32 initialAdvancesBW2(u64 seed)
{
BWRNG rng(seed);
u32 count = 0;

for (u8 i = 0; i < 5; i++)
{
count += advanceProbabilityTable(rng);

if (i == 0)
{
count += 3;
rng.advance(3);
}
}

for (u8 limit = 0; limit < 100; limit++)
{
count += 3;
u8 rand1 = rng.nextUInt(15);
u8 rand2 = rng.nextUInt(15);
u8 rand3 = rng.nextUInt(15);

if (rand1 != rand2 && rand1 != rand3 && rand2 != rand3)
{
break;
}
}

return count;
}

static u32 advanceProbabilityTable(BWRNG& rng)
{
u32 count = 0;

count++;
rng.advance(1);

count++;
if (rng.nextUInt(101) > 50)
{
count++;
rng.advance(1);
}

count++;
if (rng.nextUInt(101) > 30)
{
count++;
rng.advance(1);
}

count++;
if (rng.nextUInt(101) > 25)
{
count++;
if (rng.nextUInt(101) > 30)
{
count++;
rng.advance(1);
}
}

count++;
if (rng.nextUInt(101) > 20)
{
count++;
if (rng.nextUInt(101) > 25)
{
count++;
if (rng.nextUInt(101) > 33)
{
count++;
rng.advance(1);
}
}
}

return count;
}

private:
u64 seed;
};

int main()
{
u64 result = 0;
for (u64 i = 0; i < 100000000; i++)
result += BWRNG::initialAdvancesBW2(i);

return (int)(result % 255); // 169
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post