It depends on the how steep the hills are and how long they are.
In theory, riding on the flat should be faster. But in my experience undulating terrain is the fastest. Let me explain.
Using maths you can easily see that what effects your time over a course (hence your average speed) the most is how long you spend at lower speeds. So to set a quicker time we should improve our time on the slowest sections first.
If the road is flat then that is hard. When the hills too steep and long it is hard also.
But when the hills are short enough that I can power up them, then with extra energy expenditure I can maintain a good speed. This is psychologically aided by being able see the top and know the effort is short term.
Then, after passing the crest, comes the important bit, and this is why the hills have to be short and not too steep: apply power on the way down also.
If the downhill is followed by a flat section then by maintaining the effort level that speed can be maintained for many minutes. But if the downhill is followed quickly by another short hill, then the speed can be used to attack the next hill.
So too steep and long means too steep to climb quickly.
An example is a long shallow climb I do here. It averages only about 2% and so I can ride at about 25kph. Then I come to a 600m section which descends at over 7% then climbs at over 9%. Overall, the section rises at an average of 5%, but I average 32kph.