  We present a determination of the luminosity functions of massive young
stellar objects (MYSOs) and compact (C)HII regions within the Milky Way Galaxy
using the large, well-selected sample of these sources identified by the Red
MSX Source (RMS) survey. The MYSO luminosity function decreases monotonically
such that there are few with $L\gtrsim 10^{5}$Lsol, whilst the CHII regions are
detected up to ~10$^{6}Lsol. The lifetimes of these phases are also calculated
as a function of luminosity by comparison with the luminosity function for
local main-sequence OB stars. These indicate that the MYSO phase has a duration
ranging from 4x10$^{5}$ yrs for 10$^{4}$Lsol to ~7x10$^{4}$ yrs at
10$^{5}$Lsol, whilst the CHII region phase lasts of order 3x10$^{5}$ yrs or
~3-10% of the exciting star's main-sequence lifetime. MYSOs between 10$^{4}
Lsol and ~10$^{5}$ Lsol are massive but do not display the radio continuum or
near-IR \HI{} recombination line emission indicative of an HII region,
consistent with being swollen due to high ongoing or recent accretion rates.
Above ~10$^{5}$ Lsol the MYSO phase lifetime becomes comparable to the
main-sequence Kelvin-Helmholtz timescale, at which point the central star can
rapidly contract onto the main-sequence even if still accreting, and ionise a
CHII region, thus explaining why few highly luminous MYSOs are observed.
