The amount of sleep we get can impact our mental health in later life, a study has warned.
Sleeping too little or for too long disrupts how we think and can age the brain by up to seven years, the findings show.
Getting less or more than the six to eight hours recommended sleep increases the speed of cognitive decline and affects abilities such as reasoning and vocabulary.
The researchers warned that worsening brain function could also trigger physical decline and early death.
University of London scientists showed how changes in sleep over a five-year period in late middle age affects cognitive function in later life.
The six standard tests measured memory, reasoning, vocabulary, phonemic fluency, semantic fluency and global cognitive status.
Between 7 and 8 per cent of those who slept longer than the recommended amount fared worse in all the cognitive tests apart from short-term verbal memory.
A quarter of women and 18 per cent of men who slept less suffered a decrease in their capacity for reasoning and vocabulary.
Around seven hours of good quality sleep is fundamental to human functioning and well-being, according to the study.
Sleep deprivation and sleepiness have adverse effects on performance, response times, errors of commission, and attention or concentration.
Furthermore, sleep duration has been found to be associated with a wide range of quality of life measures, such as social functioning, mental and physical health, and early death.
Senior research fellow Jane Ferrie said: 'The main result to come out of our study was that adverse changes in sleep duration appear to be associated with poorer cognitive function in later-middle age.
'Given that our 24/7 society increasingly impinges on the lives of many people, it is important to consider what effects changes in sleep duration may have on health and well-being in the long term.'
Researchers found the ideal amount of sleep duration of seven hours per night resulted in the highest score for every cognitive measure, followed closely by six hours of nightly sleep.
Among men, cognitive function was similar for those who reported sleeping six, seven or eight hours - only short and long sleep durations of less than six hours or more than eight hours appeared to be associated with lower scores.