>>324580
See pic related.
That's a chart of muzzle speed with different barrel lengths using the same platform. Of course, it's a bolt action rifle as the filename states, so the numbers will be a bit higher than with a gas operated auto rifle.
that chart is particularly good because you have a good reasonable variety of loads and bullet weights.
Between 16.5 and 20 inches you're losing 276 Joules on average across all of cartridges with a peak on the 147gr Winchester (which is a lot like NATO's M80) of 296 J.
Between 20 and 22 (21 is a shit), you see a lose on average of 98 joules, with a max of 133 J on the heaviest cartridge (180gr).
Above that, you're dealing with an overly long weapon, 23 inches is almost as shitty as 21 in terms of energy gains; 24 is not so bad, but you're into the FUCKING LONG range of weapons.
Bearing the limit of practicality in mind, I'd say that 20 is ideal for the majority of the cartridges, 22 is not an awful idea if you're firing the heaviest shit out there; Worth saying, there is heavier than 180 and it probably does a bit better with longer barrels, but if you intend to feed your rifle with a wide variety of bullets, twist rate might become an issue, making you lose a bit of performance (be it accuracy or muzzle speed) with one cartridge or the other.
You also need to take into account that the energy the bullet has at the muzzle is not the same you will have down range, that relatively small difference between 3184J from a 16.5 vs a 3480J from a 20 translate into more than a handful extra meters of effectiveness on taking down shit.