The only way a gun might be more efficient is in a situation with total air supremacy like Iraq or Afghanistan, and the targets would ideally be in the open.
In modern great power military conflict, if you get to the point where you can fly F-16s or a hypermaneuverable AI dogfighting drone into visual range against an opponent [with inferior planes] who's out of missiles, haven't you already won?
If the opponent isn't inferior in dogfights, then instead of using ~$1mil munitions as expendables, you're using planes (and possibly pilots) as expendables. That's not a good or economical trade-off when missiles are as good as they are today.
It's an ugly truth that you need an advanced infrastructure to build these sophisticated weapons, that level of infrastructure is easily destroyed.
Fundamentally, those cost a few thousand dollars in components at most. The reason the end products are expensive is that they are not optimized for mass production. Which, for better or worse, is starting to change. My 2c.
https://en.m.wikipedia.org/wiki/AIM-120_AMRAAM
Do you believe a jet fighter drone with a gun can be manufactured with sensing, computing, landing, takeoff, and AI for a million, or even a million per kill vs fighter craft? I do not.
Maybe you'll want AI for loitering munitions, for air to air a fighter drone is going to look more like a first stage for a multi-missile launch platform. It'll fly a while, loiter, volley against aircraft or missiles, then return for rearm and recover.
In a large scale war, I’m not so sure. A fire-and-forget weapon with a 40km range is expensive and might be a scarce resource. Bullets are much cheaper, and if the AI can fly itself close enough to shoot you, it can do so inexpensively.
And this is not to mention that a 40km range weapon requires targeting that works at 40km.