#Optimization help

33 messages · Page 1 of 1 (latest)

lavish pilot
#

Any optimization help? I want to take a Vector of u8, split it in twain and put it in a new vector. The original vector comes from fs::read, where im reading arbritary files into a vector, and then i want to manipulate that vector in a format I want

let binary_vector = [This will always yield a Vec<u8> of varying length]
// A test I did had 6 767 149 bytes, which is, yes, a lot, but I want to be able to do this quickly :p
let mut sliced_vector: Vec<u8> = Vec::new();
for byte in binary_vector {
    // Get the 4 most significant bits
    sliced_vector.push(byte & 0xF0);
    // Get the 4 least significant bits
    let mut x = byte & 0x0F;
    // Shift 4 spots ahead so we can concern ourselves only with grabbing the most significant
    // bits when we add text to our image.
    x = x << 4;
    sliced_vector.push(x);
}```
ripe merlin
#

Creating the sliced_vector with vec::with_capacity would be the most obvious one

lavish pilot
#

gotcha, ill do some real quick profiling

lavish pilot
#

slightly faster, yes

#

Although weird, in an isolated environment, its much faster than in my project.

#

doesn't make a lotta sense but alright

#

its definitely working in that for loop in both my isolated env and in the project im doing

#

this is my project, the lines im talking about is 29 to 35

lavish pilot
ripe merlin
#

Is it compiled in release mode

lavish pilot
#

wait...

#

nope

#

unoptimized debug build, both of em

#

Okay, this is weird.

#
    let time1 = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards")
        .as_millis();
    for byte in binary_vector {
        // Get the 4 most significant bits
        sliced_vector.push(byte & 0xF0);
        // Shift to the left to get least significant bits in the most significant part
        let x = byte << 4;
        sliced_vector.push(x);
    }
    let time2 = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards")
        .as_millis();
    println!("Time taken: {}", time2 - time1);
#

it prints out the "time taken" thing almost immedially

#

and then does SOMETHING

#

i have no clue what it does

#

right after the time taken thingy

#
println!("Time taken: {}", time2 - time1);
    print!("Starting to read image. ");
    let img = ImageReader::open(path).unwrap().decode().unwrap();```
#

so it should print "starting to read image" immedially, right?

#

but nope

#

if i print something inside the loop, it does print and does continue printing

#

but why does the "time taken" print immedially as if it skipped over the for loop?

#

Some more testing,

Byte: 97
Byte: 98
Byte: 99
Byte: 100
Byte: 101
Byte: 102
Byte: 10
Byte: 0
Time taken: 0

Byte: is the print ive putten inside the loop

#

Okay so, it does the for loop, print out time taken

#

but "starting to read image" takes suspciously long time

#

i might have looked at the wrong part to optimize but its still weird

#

oh.

#

Note that stdout is frequently line-buffered by default so it may be necessary to use io::stdout().flush() to ensure the output is emitted immediately.

#

😮‍💨

#

I've found the source to why it takes long time, it wasnt the loop, but it wasn't being flushed before way later