You don't reinvent the wheel, unless you have valid reasons to do it.
If you are aware of the actual implementation, but believe you can do better (the second reason from the list), than you start by explaining to yourself what exactly you're imagining improve over the existent solution. Then,
- Either you solve the problem,
- Or you find yourself unable to solve it,
- Or, which is, I imagine, your actual case, you discover that you can't clone the existent solution in the first place, before even trying to add your unique features or do things better.
In the last two cases, you simply check constantly your goals and your actual state.
- Don't code just for the purpose of coding. Code with a goal in mind.
- Don't spend hours writing untested and untestable code. Start by writing tests, then code itself.
Don't blindly try achieving an imprecise goal. Write, if need, the precise functional and non-functional requirements. Having the last ones is especially important. If you're reimplementing something, make sure you have enough non-functional requirements which control the performance of your solution compared to the one you clone, how well is it documented, how extensible is it, etc.
Gathering and writing those requirements, by the way, may push you into a deeper exploration of the solution you're cloning, which finally may lead you to the conclusion that your actual need is already solved by it, or that instead of reinventing everything, you may just extend the existent solution. Here's an example of both finding unknown features and extending the solution.
Finally, how long will you persist trying until you understand you're on a wrong way depends on your personality. I myself wasted four hours this Sunday trying to implement something in Python (while I never used Python before), to finally understand that I completely failed, and that the only reasonable way to do my job is to discard everything I've done for hours, and solve the business problem in a language I use daily for the last six years. Other people would have spent maybe a day. Others would have abandoned after an hour.
It also depends on the context. I really wanted to try something "real" in Python. If it was a language I don't have a good opinion, I would have abandoned after the first issue, blaming the language.