Does the length of a gun barrel have any effect on the power(?) of a bullet?

One of the firearms periodicals (Guns & Ammo, most likely) once had an article about the effect of barrel length on muzzle velocity. They got the longest barrel they could order, chambered for a rather low-powered cartridge (.32 ACP, IIRC), and fired an bunch of shots through a chronograph while cutting the muzzle end off in one-inch increments. The conclusion was that the longer barrel made the bullets faster, up to a point at which the propellant gases stopped expanding and friction started slowing the bullet down. Optimum was somewhere around 30 inches, I think.

The optimum barrel length for a .50 BMG is really long, because it uses relatively slow-burning powder.
There’s a theory that shorter barrels are more accurate, because they’re stiffer and less likely to twist a tiny bit due to the inertial forces of the rifling. This is more prevalent among users of slow cartidges, like the .45-70, that wouldn’t gain much from a longer barrel.