Image of man in bed with head on pillow, looking at alarm clock

Working out when you’re older can protect against cognitive decline. But if you’re not getting enough sleep, you could reduce the benefits of exercise, a new study found.

A study in The Lancet Healthy Longevity examined cognitive function in 8,958 people aged 50 over a 10-year span. All of the people were located in England. The researchers looked at how sleep and exercise habits impacted their brain power over time.

When people were more physically active but had less sleep — under an average of six hours a night — they experienced cognitive decline more quickly. In other words, over a 10-year span, the active ones who slept less had the same cognitive function as their peers who didn’t exercise as much.

“Our study suggests that getting sufficient sleep may be required for us to get the full cognitive benefits of physical activity,” Mikaela Bloomberg, who is earning her PhD from University College London’s Institute of Epidemiology & Health Care, said in a statement. “It shows how important it is to consider sleep and physical activity together when thinking about cognitive health.”

The researchers split participants into three groups: A short night of sleep was under six hours, an optimal night was between six and eight hours, and a long night of sleep was over eight hours. 

Sleeping between six and eight hours per night and undergoing higher levels of physical activity were linked to better cognitive function, the researchers found. People who were more physically active had better cognitive function no matter how long they slept when the study began. This changed over the 10-year span, as people who were more physically active but received under six hours a night had more quick cognitive decline. It happened more so for people in their 50s and 60s, but people over 70 seemed to maintain their cognitive benefits from exercising–even if they got fewer Z’s a night.

Other studies have mostly looked at how physical activity and sleep could affect brain power but lacked the long-term monitoring to see better insights.

“We were surprised that regular physical activity may not always be sufficient to counter the long-term effects of lack of sleep on cognitive health,” Bloomberg said.