I would not even mention your perception that you were told not to do it (a fact which is debatable based on your report of what he actually said). I wouldn’t apologize unless sorely pressed to it (though then genuinely for having done something injurious that you didn't intend), and I definitely wouldn’t say I’d done it on my own time, as if you’re salaried then all the energy you spend in work belongs to your company, and if you’re paid hourly then it was illegal to not report the hours.
I'd play it like this:
"I was curious about how you said the bottleneck wasn't possible to remove in this software. Since it's such a problem for our business to have this bottleneck, I did a little research and it seemed there could be a way, and I decided to spend a little time checking if it could work. I spent only 2 hours on this yesterday, and I think I've managed to remove it. What do you think? Is this a viable strategy?"
Focusing on who told whom what or that you knew you were going against him is not the path to success. Focus instead on the business value provided: big problematic bottleneck removed, only 2 hours spent, openness to the boss's opinion on the result and going forward.
Your boss is an advisor. It's poor management practice to be giving these kinds of orders anyway. You were hired for your programming knowledge, and you used it. You're a valuable employee because of your keen mind. Don't let pointy-haired bosses get in the way of you using it to the best of your ability.
If your boss makes a big deal out of it, then you know you're in the wrong company or certainly under the wrong boss, and in that case I would then confidently predict that this kind of thing will come up again and again with him, and will be a long-term problem. Find out NOW what kind of company you work at, and decide if you can live with it. It's also the better strategy to help your boss find out NOW what kind of employee you are, and decide if he can live with it. If you don't show initiative now and you do everything your boss orders you to do, even when it's stupid to obey, or even when it's low cost to find out more information, then your boss will expect this kind of compliance from you in the future and you'll be locked in forever.
I'm not suggesting you just go on a rampage and be a wild west cowboy, ignoring your boss. But you have an opportunity here to create a different kind of relationship where he only orders you to do high-level things that are actually valuable to the company, and otherwise he functions in an advisory and facilitating capacity, leaving the rest of the important pieces (including how you spend your time, and your choices on implementation) up to you. If you meet deadlines and the company's objective needs, you should be free to work as you please.
Regarding deception
Deception is an attempt to cause someone to believe falsehood. Your boss knows what he told you. I think it unlikely that using my suggested approach will result in him believing falsehoods about the situation. You in fact don't know how strongly he feels about obeying his suggestions as orders, and this is how you find out. If he has no problem with what you did, you'll know, and if he has a problem, you'll know that too. If he does overlook his apparent command to you because you succeeded, this is valuable information.
It is easier to get forgiveness than permission. By not asking for permission you showed you don't need him to be telling you what to do all the time. Constantly checking in with your boss for permission is a very clear meta-message that you don't trust yourself to make your own decisions. He didn’t order you not to do it.
If your boss takes it personally
I would say something like this, very calmly and rationally, looking him directly in the eye (but not in a challenging way), while projecting an attitude of compliance to his wishes, but acting as though he will simply agree with you, thus shaping the situation in a way favorable to your desired outcome:
"Joe, I truly meant nothing personal by pursuing this. I saw your advice as you just doing your job: trying to achieve maximum utility to the company by helping me not waste time. I did what I thought was my job by taking a calculated risk and spending a deliberately short amount of time investigating it—and that paid off. Should I understand that in the future you don't want me to take these kinds of calculated risks when I see value to the company in pursuing them wisely?"
You could further take the opportunity to discuss the kind of relationship you and your boss plan to have going forward:
"I'd like to talk about the kind of relationship you and I are going to have. It's my understanding that you hired me for my knowledge, skill, and brains as a software developer. It's going to be hard for me to do the best possible job here if I am going to be given hard constraints that dictate how I have to come up with my solutions. Please understand that of course your input is valuable to me because you have been here longer, and due to your position you know all sorts of important things I don't, such as deadlines, customer concerns, politics, and so on. Yet, I need to be able to apply my expertise as well. I'd like to hear from you the top-level business needs that dictate the criteria for calling a finished project a success, and I definitely want to hear your advice and thoughts, but then I'd like you to leave it up to me to achieve that success. If I fail, then we can always talk about you having greater involvement in directing my steps, but if I succeed (as I did here), it seems like this would be the best way to get the most value from my work."
"Is this a way of working together that you could feel comfortable with, going forward? Could we try it out for a month or so and then check back in to see how you think it's going? I'm always open to suggestions for improving my performance and my final work product."
You could follow this up by discussing with him the video Dan Pink on motivation – mastery, autonomy, and purpose. Talk about those things, then forward him the link. Explain that having autonomy and being allowed to practice your mastery are crucial to your motivation at the company, and that you'd like him to grant you those things so you can be the best employee possible.
You might ask him to watch Greatness by ship captain David Marquet or a longer version (there are other versions as well, look at the related videos). You might even get him to watch Great Leaders Serve Others.
It's not enough to sit back and let your boss manage you at his whim, however he wants. To be successful you have to manage him, too. Help him learn how to provide what you need to make it easiest to do a top notch job.