Medusa, you pose good questions, but there are so many variables that the short answer is "It depends!"
1. The
propellant used in the cartridge is a major factor. One can get propellants burning at different rates. A fast-burning propellant will be "used up" within a few inches of barrel, and technically, one could expect the friction of the bullet against the bore to slow down the bullet from the point where the propellant gas is no longer expanding, until the bullet exits the bore. A slow-burning propellant will provide gas and expansion for a longer period, so that the bullet will exit the bore with the gas still accelerating it. Thus, a fast-burning propellant in a 16" barrel might be very efficient, but a slow-burning propellant in a 26" barrel might be even more efficient.
2. The
quality of the bore is also a factor. A rough bore will produce more friction against the passage of the bullet than will a polished bore. To illustrate how this can be a factor in even the finest barrels,
Microlon Gun Juice, when properly applied to treat a barrel, will often produce muzzle velocity gains of 100 fps or so in most rifles, even those with mirror-smooth bores to begin with. I've treated several of my rifles with this stuff, and was amazed to see the improvement. Go figure!
3. The
bullet material is another factor. A soft lead bullet will have a different coefficient of friction compared to a copper-jacketed bullet. The lubricant (if any) used on a bullet will also have an effect.
4. Finally, the
position of the powder in the cartridge will have a profound effect. You can easily measure this if you have access to a chronograph. If you tip the weapon forward (i.e. muzzle down), so that the propellant in the cartridges is moved by gravity to the bullet end of the cartridge, and then fire, you'll record a given muzzle velocity. If you tip the weapon backward (i.e. up), so that the propellant is moved to the primer end of the cartridge, and then fire, you'll record a considerably greater muzzle velocity.
With so many variables, it's hard to come up with a general rule. Most authorities would agree that for a .22LR, 16" is the optimum barrel length: thereafter, there's not enough gas to keep bullet acceleration going, and the bullet starts to slow down in the bore. This is not necessarily a bad thing, of course: for target shooting, you aren't worried about maximum velocity, and the longer sight radius of a long barrel is a major advantage, even if it slows the bullet. However, if you're shooting small animals, and want bullet expansion, then velocity becomes more important. Here, a shorter barrel, with greater efficiency and velocity, is important.
For .223 and .308, I think the minimum length of barrel for optimum performance would be 16", and the maximum 20", with the usual propellants. Of course, you can change the propellant to give better performance in shorter or longer barrels, but most factory ammo will function best between these two lengths. There is a noticeable difference in muzzle flash between 16" and 20" in these calibers: fire the same round at night out of the two barrel lengths and you'll see what I mean. The longer barrel lets most of the propellant burn up, resulting in a much smaller muzzle flash, whereas the shorter 16" tube provides a bigger muzzle flash as unburned propellant burns up outside the barrel.
Hope this helps.