To find out, the researchers gave the flies a learning test. They began by teaching the flies to be scared of a particular smell. They would put a smelly piece of paraffin into the tubes where the flies lived, and after 30 seconds, the scientists gave the tubes a violent shake. After many such experiences, the flies learned to associate the smell with the shaking.

An hour later, the scientists tested how well they had learned. The flies were put in a tunnel that ended with a T intersection. From one side they smelled the dangerous odor, and from the other they smelled a harmless one. Dr. Hollis and Dr. Kawecki then observed which way the flies walked.

The results were stark. The monogamous flies were much more likely to wander toward the dangerous smell than the polygamous ones. In other words, they had done a much worse job of learning.

“I think this is a compelling and interesting study,” said Emilie Snell-Rood of the University of Minnesota, who was not involved in the research. The experiment, she said, suggests that the struggle to find a mate favors the evolution of better learning.

The evolution of learning remains a puzzle for scientists. A smart animal can learn how to find more food or how to avoid predators. But if learning were such an unalloyed good, then one might expect all animals to be as smart as we are.

They are not because there is a cost to learning. Dr. Kawecki and his colleagues have found that flies that have been bred to be good learners are more likely to die when competing for scarce food with regular flies. Even when they’re not threatened with starvation, their life span is 15 percent shorter than average.