The longest I've worked at one stretch was 7 days, only taking bathroom breaks and food breaks twice a day (teeth brushing breaks after food breaks) only partially napping for 15 minutes while waiting for my food order. (there was no shower at the lab, so I only hopped into clean clothes every day). At day two I was working like a maniac, day three I was back the same productivity level as day one, day four time started to go faster and slower at the same time and I started to have mild hallucinations from lack of sleep, day five and six I couldn't remember much of, time was leaping and there was code in my code-base that I don't remember writing, code that worked, but I don't remember writing it. I started losing the ability to speak unless I spoke very slowly. Day seven, I handed in two projects (I've finished several months' work in just seven days, not because I was slacking, varsity workload was just insane, I only had 7 days of the 3 months to finish it or otherwise repeat the subject)
... the world was moving in slow-motion, cars seems like they are standing still and then re-appearing in other places. I would have blackouts for a second or two according to my watch and have lucid dreams in those few seconds of being semi-unconscious, but those lucid dreams would feel like half a day and often times I would solve very complicated problems during those lucid dreams and struggle to remember all of it. It was Monday 8 am, I'm sitting in my first class of the day after hardly getting any sleep since the previous Monday, the professor would ask me questions, I would black out for a second and then somehow manage to answer him a second later that feels like 6 hours has passed. I finally got 8 hours of sleep that night following that 7 day work-marathon and then I had to switch to Uberman Sleep in order to slowly get back into normal sleep mode again, the next 6 months was mostly working 1-3 days at a time surviving on 15-30 min naps and having one 8 hour sleep night twice a week until I eventually started moving back to 2-4 hours sleep a day.
Certain things can be done in one long stretch (repetitive work or work that doesn't require much thought depending on your skill level and what you would consider deep thought), anything that requires insight and deep thought - after a very long stretch, your brain is pretty much shot and you'll start seeing errors creep into your work. You need to decide how much errors you can tolerate before the quality of your work starts to suffer. Sometimes going to insane lengths to get a project done is needed, sometimes the mess you make is not worth it.