There are far far too many variables involved, in particular the acceleration distance required for clean-up, and the loss of gradient involved.
(1) If it is a near-in obstacle that is of concern, maintain the flap setting and the appropriate lower speed until the obstacle is passed.
(2) If there are no 'near-in' problems, accelerate to Vx as quickly as possible when prudently safe after takeoff, as you have suggested.
As stated, far too many variables to categorically state an 'all purpose' best gradient profile, but a 'finger in the wind' guesstimate puts the crossover point somewhere between 20 and 30 miles for a typical heavy jet aircraft.
Regards,
Old Smokey