Should I ask for a letter of recommendation when my performance wasn't great at the beginning?
I think that the poor performance on the first exam might actually help you to get a good recommendation.  In general, faculty like for someone to learn from their mistakes and make up for it.  

Or, the faculty member may not remember your performance on the first exam and just go by the final grade.  Especially if you ask after the semester/quarter is ended.  

Other things that can help you get a good recommendation include asking for help in a polite and formal way (e.g. without complaining) by visiting during office hours, and asking for specific assistance with something.  

If you have more classes and have the same instructor/professor, then do well there too.  Ask them, in person, "Would you provide me with a positive recommendation for graduate school" and if they seem to grimace or have another negative expression, immediately ask "How can I earn a positive recommendation from you?"  People, especially faculty, want you to earn.