Depression keeping you up at night? Staying awake may be the key to rapidly improving your mood.

Researchers at the Perelman School of Medicine at the University of Pennsylvania looked back at decades’ worth of studies on sleep deprivation and concluded it can temporarily improve symptoms of depression in up to 50 percent of people.

All forms of sleep deprivation, ranging from partial (20 to 21 hours without sleep) to total (up to 36 hours), were an effective anti-depressant for patients across demographics, according to the analysis of 66 English-language studies on the topic from 1974 to 2016. What’s more, patients reported feeling better within as little as 24 hours after treatment.

“These studies in our analysis show that sleep deprivation is effective for many populations,” the study’s lead author, Elaine Boland, a clinical associate and research psychologist at the Cpl. Michael J. Crescenz VA Medical Center, told Penn Medicine.

“Regardless of how the response was quantified, how the sleep deprivation was delivered, or the type of depression the subject was experiencing, we found a nearly equivalent response rate,” she said.

The study, first published in the Journal of Clinical Psychiatry, acknowledged that more research needs to be done to determine why lack of sleep eases depression so rapidly.

“More than 30 years since the discovery of the antidepressant effects of sleep deprivation, we still do not have an effective grasp on precisely how effective the treatment is and how to achieve the best clinical results,” study senior author Philip Gehrman, an associate professor of psychiatry and member of the Penn Sleep Center, told Penn Medicine.

One 2015 study, published in Neuron, found that sleep deprivation affects a receptor in the frontal lobe that is also affected by tricyclic antidepressants and ketamine.

The potential link between sleep deprivation and mood enhancement is nothing new.

German psychiatrist Johann Christian August Heinroth noted the connection 200 years ago, New Atlas reported.

Heinroth, who also coined the term “psychosomatic illness,” found that sleep deprivation had positive effects on patients suffering from depression, or what he called “melancholia.” Since then, doctors have experimented with several types of sleep deprivation on depressed patients.

Wake therapy, first developed in the 1970s, is sometimes administered to patients to jump-start improvement in depressive symptoms. While effective, the benefits are temporary and patients report a return of their symptoms days to a week after treatment.

Another form of sleep deprivation called chronotherapy, which combines forced wakefulness with bright light therapy, may stave off depressive symptoms longer, research suggests.

While certain depression treatments induce sleep deprivation, lack of quality sleep may be a contributing factor to the mental illness, according to Sudhir Gadh, a psychiatrist with a private practice in Manhattan.

“While sleep deprivation has proven to be a useful tool in treating depression, it is ironically what most people suffering from depression already are experiencing,” Gadh told The Post. “That’s because depressive or anxious sleep is innately lighter. It is less dream filled (less REM) and less physically restorative (less Stage III and IV).”

“Sleep deprivation may work by moving the brain towards a reset of its circadian rhythm the following night resulting in deeper sleep temporarily,” he added.

Even though sleep deprivation is effective in temporarily relieving depression, it shouldn’t be the only means of treatment.

Gadh said patients should also make lifestyle adjustments, take tailored medication and employ other evidence-based therapies.

Dr. Nina Urban, attending psychiatrist at NYC Health + Hospitals/Bellevue and clinical assistant professor of psychiatry at New York University Medical Center, says there’s not enough evidence to suggest sleep deprivation is a safe and effective means to treat depression in the long term — and may even be detrimental.

“Chronic sleep deprivation also affects the immune system and the cardiovascular system negatively, increasing the risk for other medical problems,” she said.

“Given the lack of studies looking at long-term sleep deprivation for depression, it is hard to say where a safe threshold for chronic sleep deprivation would be in which antidepressant benefits outweigh any other risks,” she added.