If you consider the relationship between piston/crank position and speed of burning of different grades of gasoline, then ignition timing and engine rpm can have a sigificant effect on gas mileage.
As an example, if you have "knocking", you can usually cure it two ways.
You can retard the timing, this allows a greater volume of combustion chamber for the burning fuel to expand into, as the piston is a little further down the bore as combustion is complete.
You could, instead, switch to a higher octane gas. The slower burn rate would have a similar effect to retarding the ignition.
This is a very simplistic view as compression ratio, bore and stroke, engine rpm and temperature all have a significant effect.
Sometimes, running premium gas will allow the engine to run cooler by reducing pressures during the early part of the combustion cycle. Hopefully, this translates into more pressure applied to the piston near the center of its stroke where it can do the most good, possibly improving gas mileage.