Every hour of sleep before midnight is worth two after midnight . Your grandparents (and great grandparents) probably adhered to that creaky adage. “The mythology is unfortunate, because there’s no pumpkin-like magic that occurs,” says Dr. Matt Walker, head of the Sleep and Neuroimaging Lab at the University of California, Berkeley. And while nothing special happens to you or the quality of your sleep at the stroke of midnight, many do wonder: What's the best time to go to bed?

Walker says your sleep quality does change as the night wears on. “The time of night when you sleep makes a significant difference in terms of the structure and quality of your sleep,” he explains. Your slumber is composed of a series of 90-minute cycles during which your brain moves from deep, non-rapid eye movement (non-REM) sleep to REM sleep. “That 90-minute cycle is fairly stable throughout the night,” Walker explains. “But the ratio of non-REM to REM sleep changes.”

He says that non-REM sleep tends to dominate your slumber cycles in the early part of the night. But as the clock creeps toward daybreak, REM sleep muscles in. That’s significant, because some research has suggested that non-REM sleep is deeper and more restorative than lighter, dream-infused REM sleep—though Walker says both offer important benefits.

What does this have to do with the perfect bedtime? The shift from non-REM to REM sleep happens at certain times of the night regardless of when you go to bed, Walker says. So if you hit the sack very late—at, say, 3 AM—your sleep will tilt toward lighter, REM-heavy sleep. And that reduction in deep, restorative sleep may leave you groggy and blunt-minded the next day.

That’s unfortunate news for nightshift workers, bartenders and others with unconventional sleep-wake routines, because they can’t sleep efficiently at odd hours of the day or night, Walker says. Shift work has been linked to obesity, heart attack, a higher rate of early death and even lower brain power. In one study, people who had experience working at night had lower scores on standardized tests of memory and processing speed than those who hadn't—and people who had a decade or more of shift work experience had such pronounced cognitive deficits that they equaled about 6.5 years of cognitive decline. Even shortened sleep has an effect, one recent study found . People who slept for five hours a night for just a week had a higher heart rate during the day. “The idea that you can learn to work at night and sleep during the day—you just can’t do that and be at your best.” Your brain and body’s circadian rhythms—which regulate everything from your sleeping patterns to your energy and hunger levels—tell your brain what kind of slumber to crave. And no matter how hard you try to reset or reschedule your circadian rhythms when it comes to bedtime, there’s just not much wiggle room. “These cycles have been established for hundreds of thousands of years,” Walker explains. “Thirty or 40 years of professional life aren’t going to change them.”

You don't have to be a shift worker to feel this

You Asked: Your Top 10 Health Questions Answered You Asked: Are Cleanses Healthy? Potions that claim to clear your body of toxins might sound alluring, but do they deliver? Illustration by Peter Oumanski for TIME You Asked: Are Cleanses Healthy? Potions that claim to clear your body of toxins might sound alluring, but do they del ... VIEW MORE

Illustration by Peter Oumanski for TIME 1 of 10

When it comes to bedtime, he says there’s a window of several hours—roughly between 8 PM and 12 AM—during which your brain and body have the opportunity to get all the non-REM and REM shuteye they need to function optimally. And, believe it or not, your genetic makeup dictates whether you’re more comfortable going to bed earlier or later within that rough 8-to-midnight window, says Dr. Allison Siebern, associate director of the Insomnia & Behavioral Sleep Medicine Program at Stanford University.

“For people who are night owls, going to bed very early goes against their physiology,” Siebern explains. The same is true for “morning larks” who try to stay up late. For either type of person—as well as for the vast majority of sleepers who fall somewhere in between—the best bedtime is the hour of the evening when they feel most sleepy.

That means night owls shouldn’t try to force themselves to bed at 9 or 10 if they’re not tired. Of course, your work schedule or family life may dictate when you have to get up in the morning. But if you can find a way to match your sleep schedule to your biology—and get a full eight hours of Z’s—you’ll be better off, she adds.

Both she and Walker say your ideal bedtime will also change as you age. While small children tend to be most tired early in the evening, the opposite is true for college-aged adults who may be more comfortable going to bed around or after midnight. Beyond college, your best bedtime will likely creep earlier and earlier as you age, Walker says. And again, all of this is set by your biology.

Siebern suggests experimenting with different bedtimes and using sleepiness as your barometer for a best fit. Just make sure you’re rising at roughly the same time every morning—weekdays or weekends. It’s fine to sleep an extra hour on your days off. But if you’re getting up at 6:30 during the workweek and sleeping until 10 on weekends, you’re going to throw off your sleep rhythms and make bedtime more challenging, she says.