maximum bullet velocity - when?

When does a fired bullet reach its max velocity? Essentially instantaneously? Or at some point along the barrel? Or just exiting?

I was taught in physics that the max velocity of a of a bullet occured at the exiting of the barrel and that it
loses velocity thereafter.

But, what if you had a long barrel (say 30 feet)and a small charge (like a .22)? Would friction forces come into play and lower the exit velocity to a point where max velocity actually occured in the barrel? Probably so. Newtons laws of motions apply to perfect conditions and usally require some compensation due to other forces such as friction and wind.

For a properly designed gun it is safe to assume that max velocity occurs at the exit of the rear of the bullet at the end of the barrel.

The point at which the gas from the chemical reaction has taken up an amount of volume such that the rate that the gas expands is no longer increasing. If you have a barrel with a large volume but a small amount of explosive then this point is somewhere in the barrel. If you have a barrel with a small volume with a large explosive then this point will be at the end of the barrel. In real life this is at the end of the barrel.