Raytracing performance

I am creating a raytracer but it runs very slowly even though the code is very simple. I would like to know how to separate my code so that it will run more efficiently.

Hi, welcome to Snap!.

Do you know about the untitled script pic block? It does a lot of the hard part for you.

I forgot to mention that it is a 3d raytracer, however, if I make a 2d one that will be useful.