Although sleep deprivation is known to impair attention in humans and other mammals, the underlying reasons are not well understood, and whether similar effects are present in non-mammalian species is not known. We therefore sought to investigate whether sleep is important for optimizing attention in an invertebrate species, the genetic model Drosophila melanogaster. We developed a high-throughput paradigm to measure visual attention in freely walking Drosophila, using competing foreground/background visual stimuli. We found that whereas sleep-deprived flies could respond normally to either stimulus alone, they were more distracted by background cues in a visual competition task. Other stressful manipulations such as starvation, heat exposure and mechanical stress had no effects on visual attention in this paradigm. In contrast to sleep deprivation, providing additional sleep using the GABA-A agonist 4,5,6,7-tetrahydroisoxazolo-[5,4-c]pyridine-3-ol (THIP) did not affect attention in wild-type flies, but specifically improved attention in the learning mutant dunce. Our results reveal a key function of sleep in optimizing attention processes in Drosophila, and establish a behavioral paradigm that can be used to explore the molecular mechanisms involved.