How is experience gained from gym battles calculated?
From my experimentation, you get more experience from using lower-CP Pokemon. So if you have win with a type advantage instead of sheer CP then you get more experience for that.
When I battle the same friendly gym repeatedly to try to join, I find that the experience is not random, but only varies with what I use. So the time and HP seemed to make little difference, since missing a dodge didn't change my experience gain.
I would imagine opposing gyms follow the same patterns, though I haven't gotten a read on how other variables (number of Pokemon used, team composition even if you only use one of them, etc.) affect things.