You do a few takes and then comp the final as needed.
Just playing something over and over and over and over until you nail a single
perfect take...is a TOTAL waste of time and is more about luck than pure perfection.
If you COULD play it perfect, you would have done it on the first take.
Takes+comping is done at the highest pro levels.
Don't think pros always nail it in one take!
But remember...the more takes you keep, the more editing time will be needed to comp them.
I usually go for 3 takes...drums, vocals, leads, whatever...but I've done 5 on occasion when I felt I could actually improve *substantially* on the first 3...and then come editing/comping time, I audition all 5, put aside the 2 worst ones (just in case I may need some part from them) and then comp from best 3.
Most people run out of steam by the 5th take...and then you're just torturing the thing, trying to force it out.
Sometimes...if you really want to beat on it for 20-30 takes...you can hit a short high peak (a.k.a. moment of brilliance) again (like getting a brief second wind in sports) after going through a bunch of ugly takes...but then you go down fast after that (you have to also suffer through the ugly takes until/if you can even find a brief peak).