It is NOT just when the gunpowder burns that velocity peaks but when the gases stop expanding. The gases DON'T stop expanding (pushing on the bullet to accelerate it faster) until well down the barrel. The ubiquitous .22 LR maxes out velocity in about 16-18 inches of barrel. A 4" gun barrel normally yields more velocity than the same cartridge in a 2" barrel, likewise a 6" barrel normally more than 4" barrel, etc. until the gases stop expanding and pushing hard enough on the bullet base to continue acceleration. The limit in barrel length required for the max velocity depends on the caliber, barrel length, case capacity, and pressure level of load among other factors.
Good shooting and be safe.
ps: Gun powders don't always burn completely in short barrels either. Why do you suppose unburned powder granules are shot out of the barrel with loads using "slow" powders in short barrels?