#Can i make my code faster???

100 messages · Page 1 of 1 (latest)

bitter salmon
#

on that big input???

#

lol

stone juniper
#

yeah

#

before it ran like 1m30s

bitter salmon
#

hmm what have u actually done...

#

hash

stone juniper
#

made is_part significantly faster :P

bitter salmon
#

oh

#

ok

#

oh so operator== is for .find

stone juniper
#

well not quite, how it works with these kind of containers it seperates it on hash, but you can have hash collision so you need a way to compare coords

#

so you need both for unordered_set

bitter salmon
#

umm is partsSet.insert working as push_front??

#

it should push value at front

stone juniper
#

no, not quite

#

the order in partSet is completly "random"

#

however it's just used to check if a coord matches any of the parts

bitter salmon
#

oh

#

it makes sense

stone juniper
#

though I didn't test it just saying

#

just ran it

#

but I think it should be fine

#

as you don't modify values anywhere, and im mirroring pops and pushes in the set

bitter salmon
#

where is actually this hash used?

#

ide shows me

#

Struct 'hash<Coord>' is never used

stone juniper
#

it's not called directly

bitter salmon
#

oh ok

#

its working very fast

#

thank you very much

stone juniper
#

do check if this is actually correct

bitter salmon
#

ok

stone juniper
#

I did not verify the output

bitter salmon
#

why i didnt heard about std::unordered_set

#

is it like faster than array but unordered???

stone juniper
#

no it's quite distinct

#

unordered_set is essentially a hased value set where everything is unique

#

there is also a regular set what is sorted

bitter salmon
#

can unordered_set or set be faster than array???

#

faster in iteration

#

when i dont need to know order

#

what will be better

#

because if its unordered

#

then it dont need to order it

#

so it can be faster

stone juniper
#

well it's most likely gona be slower to itterate

#

or well to do a full itteration, but the neat thing is with this you don't have a linear itteration

#

on average isPart is constant time at worst linear

#

before it was always linear

bitter salmon
#

ok

#

also what type of container u will recommend for fruits??

#

look what fruits does

#

it only temporarily saves data

stone juniper
#

honestly tthat seems fine

bitter salmon
#

deque??

stone juniper
#

nah never

bitter salmon
#

std::deque<Fruit>

#

im using deque currently

#

which is not good

stone juniper
#

imho that list never was an issue

bitter salmon
#

yes but it will be good if i will know for the future

stone juniper
#

look at the timing here

bitter salmon
#

yes

stone juniper
#

the only 2 big things, where isPart and timing

#

the rest is irrelevant

bitter salmon
#

yes but it will be good if i will know for the future what is faster container type for this

stone juniper
#

std::vector generally

bitter salmon
#

for just holding elements and iterating once

bitter salmon
#

isnt array faster than vector??

stone juniper
#

it doesn't make sense for the board to be a dequeue either

#

not realy

bitter salmon
#

ok

stone juniper
#

the difference is constraints, arrays need to be compile time constant

#

vectors are dynamic sized

#

what well comes at a cost, but itterating speed should be basically the same

bitter salmon
#

yes but in this case im sure what will be size of fruits

#

so vector is unnecessary

#

array can handle that better

#

there is also std::array

#

hmm

#

also i can delete new allocated array in good place

leaden cragBOT
#

@bitter salmon Has your question been resolved? If so, run !solved :)

bitter salmon
#

thanks for your help

#

!solved

leaden cragBOT
#

Thank you and let us know if you have any more questions!

bitter salmon
#

!unsolved

#

@stone juniper i have found bug

#

its not finding coord

#

actually partsSet

#

is not adding everything

#

thats because there is moment when there are 2 same coords

#

when snake is going into its own tail but is not actually like killing itself

#

everything works except this

#

wait i know why... when it happens i cant remove element

#

because its already not adding to this unordered_set

#

!solved

leaden cragBOT
#

Thank you and let us know if you have any more questions!

distant gorge
#

weird how much conversation happened after literally giving the superset of the solution thonk