45 ACP
Note most barrels are not sold by the actual barrel length (Bolt face to crown). In these tests the barrels are the actual barrel lengths measuring bolt face to crown.


I took BBTI's table and ran an average of the numbers then did a percent change by adding 1" of barrel length.
For 45 ACP it looks like 12" is the best length for a compact carbine, while still getting good accuracy. Also the bullet should be nice and stable by the time it goes through 12" of rifling.
For a pistol:
Look at common popular models, and you will see the barrel lengths are all 4-5". Pistols with 6" barrels (HK Mark 23 at 5.9") are just a bit ridiculous for a side arm. 4-5" is the best size wise for the shooter and provide the best ballistics.
9MM

For 9mm I have always heard that 10" is the best barrel length for a carbine. The numbers seem to back that up. I personally am running an 8" barrel, which I think is a great length for an AR pistol.

For 10MM 13" might be a bit long, so I'd go with a 10" barrel for compactness. You don't really lose much going down to 10"