It takes just 15 generations under these conditions for the flies to become genetically programmed to learn better. At the beginning of the experiment, the flies take many hours to learn the difference between the normal and quinine-spiked jellies. The fast-learning strain of flies needs less than an hour.

But the flies pay a price for fast learning. Dr. Kawecki and his colleagues pitted smart fly larvae against a different strain of flies, mixing the insects and giving them a meager supply of yeast to see who would survive. The scientists then ran the same experiment, but with the ordinary relatives of the smart flies competing against the new strain. About half the smart flies survived; 80 percent of the ordinary flies did.

Reversing the experiment showed that being smart does not ensure survival. “We took some population of flies and kept them over 30 generations on really poor food so they adapted so they could develop better on it,” Dr. Kawecki said. “And then we asked what happened to the learning ability. It went down.”

The ability to learn does not just harm the flies in their youth, though. In a paper to be published in the journal Evolution, Dr. Kawecki and his colleagues report that their fast-learning flies live on average 15 percent shorter lives than flies that had not experienced selection on the quinine-spiked jelly. Flies that have undergone selection for long life were up to 40 percent worse at learning than ordinary flies.

“We don’t know what the mechanism of this is,” Dr. Kawecki said.

One clue comes from another experiment, in which he and his colleagues found that the very act of learning takes a toll. The scientists trained some fast-learning flies to associate an odor with powerful vibrations. “These flies died about 20 percent faster than flies with the same genes, but which were not forced to learn,” he said.

Forming neuron connections may cause harmful side effects. It is also possible that genes that allow learning to develop faster and last longer may cause other changes.

“We use computers with memory that’s almost free, but biological information is costly,” Dr. Dukas said. He added that the costs Dr. Kawecki documented were not smart animals’ only penalties. “It means you start out in life being inexperienced,” Dr. Dukas said.