Two things:
The first time you run the function the time will include the compile time of the code. If you want a apples to apples comparison with a compiled function in Mathematica you should run the function twice and time the second run. With your code I get:
elapsed time: 1.156531976 seconds (447764964 bytes allocated)
for the first run which includes the compile time and
elapsed time: 1.135681299 seconds (447520048 bytes allocated)
for the second run when you don't need to compile.
The second thing, and arguably the bigger thing, is that you should avoid global variables in performance critical code. This is the first tip in the performance tips section of the manual.
Here is the same code using local variables:
function fdtd1d_local(steps, ie = 200)
ez = zeros(ie + 1);
hy = zeros(ie);
for n in 1:steps
for i in 2:ie
ez[i]+= (hy[i] - hy[i-1])
end
ez[1]= sin(n/10)
for i in 1:ie
hy[i]+= (ez[i+1]- ez[i])
end
end
return (ez, hy)
end
fdtd1d_local(10000)
@time fdtd1d_local(10000);
To compare your Mathematica code on my machine gives
{0.094005, Null}
while the result from @time
for fdtd1d_local
is:
elapsed time: 0.015188926 seconds (4176 bytes allocated)
Or about 6 times faster. Global variables make a big difference.