What affects the speed of my code?

My code is searching a game tree and I'm trying to figure out why it is so slow using the profiler. Do I look at CPU or memory or GC? I was told that heap allocation is bad as my game tree is huge and constantly cloning heap memory will slow me down significantly.
32 Replies
ElectricTortoise
ElectricTortoiseOP4d ago
For some reason the profiler has the memory blank
No description
ero
ero4d ago
well this view doesn't really "profile" anything. what you want to do (and i believe have already done in another post) is actually run your code with the profiler attached, and then (perhaps repeatedly) run the code that's taking too much time for your liking
ElectricTortoise
ElectricTortoiseOP4d ago
how would that work?
ero
ero4d ago
how do you mean?
ElectricTortoise
ElectricTortoiseOP4d ago
rn i just do Alt+F2 and tick the profiler box how do i attach the profiler to the code
ero
ero4d ago
alt+f2 seems right
No description
ElectricTortoise
ElectricTortoiseOP4d ago
im just ticking the boxes
No description
ElectricTortoise
ElectricTortoiseOP4d ago
the blank one is this
No description
ero
ero4d ago
and then you start it
ElectricTortoise
ElectricTortoiseOP4d ago
i did, this is what came back i also have one for CPU
ero
ero4d ago
well your app is still running, the profiler evaluates the results after you're done profiling
ElectricTortoise
ElectricTortoiseOP4d ago
oh then why does the CPU have results but not the memory
ero
ero4d ago
i would personally tick both memory and cpu that i can't answer
ElectricTortoise
ElectricTortoiseOP4d ago
alright can you tell me what things in general cause code to be slow? like nested loops or cloning heap memory etc.
Jimmacle
Jimmacle4d ago
any work takes time the profiler can help you identify specific areas of code that are taking a long time to run
ElectricTortoise
ElectricTortoiseOP4d ago
Do I look at CPU or memory or GC?
Jimmacle
Jimmacle4d ago
if you care about execution time then CPU is the main one to look at but depending on the complexity of the code you may just be better off analyzing the algorithm you're using with your brain
ero
ero4d ago
your implementation is what determines how slow or fast some code is, it's difficult to think of "general things that make code slow" it can have a million causes
ElectricTortoise
ElectricTortoiseOP4d ago
hm good idea
Jimmacle
Jimmacle4d ago
searching a tree should be relatively fast as far as searching collections goes assuming it's actually sorted and not just a random graph of objects
ElectricTortoise
ElectricTortoiseOP4d ago
right now i'm counting every single node on the tree
Jimmacle
Jimmacle4d ago
can you share the code?
ElectricTortoise
ElectricTortoiseOP4d ago
there's quite a bit
Jimmacle
Jimmacle4d ago
just the part doing the counting that shouldn't be much code
ero
ero4d ago
well, you profiled it right? share the method(s) that took the most time/you want to improve
ElectricTortoise
ElectricTortoiseOP4d ago
No description
No description
ElectricTortoise
ElectricTortoiseOP4d ago
CPU usage was spread evenly across these 4 functions, with a huge empty space for the remaining 1/5
No description
ElectricTortoise
ElectricTortoiseOP4d ago
this is MakeMove()
ero
ero4d ago
okay so it doesn't just count the nodes does this method even return anything other than 0?
ElectricTortoise
ElectricTortoiseOP4d ago
it counts the legal nodes this is a chess engine
Jimmacle
Jimmacle4d ago
i mean, determining what's legal is more than counting
ElectricTortoise
ElectricTortoiseOP4d ago
oh i suppose you are right determining legal is IsInCheck()

Did you find this page helpful?