dThere is no "perfect" length of time. Each batch varies. My rule is 10 days to two weeks for anything that doesn't need to clear--like porter, stout,etc. If it needs to clear then it gets three weeks in the fermenter and another three weeks in the bottle. Of course, these are only for the average or low gravity batches. High gravity are treated different. They usually get three weeks to six weeks in the fermenter (depending on strength and color) and then are aged in package.
Keep in mind that none of these are a rule. I pull samples, taste and look to determine when the beer is ready.