I'm trying to calculate the normal vectors and the tangent vectors at the discrete points of a suface. For example:
f[x_, y_] := (Cos[x] Sin[y])/2. (* The surface.*)
fx[a_, b_] :=
Module[{x, y}, D[f[x, y], x] /. {x -> a, y -> b}]
fy[a_, b_] :=
Module[{x, y}, D[f[x, y], y] /. {x -> a, y -> b}]
normalVector[a_, b_] :=
Module[{x, y},
{-D[f[x, y], x], -D[f[x, y], y], 1} /. {x -> a, y -> b}]
tangentVector[x_, y_, θ_] :=
{Cos[θ], Sin[θ],
fx[x, y] Cos[θ] + fy[x, y] Sin[θ]};
xr = yr = 1;
n = 20;
hx = 2 xr/n;
hy = 2 yr/n;
hθ = 2 Pi/n;
AbsoluteTiming[
Table[
nV = normalVector[x, y];
tV = tangentVector[x, y, θ];
{nV, tV},
{x, -xr, xr - hx, hx}, {y, -yr, yr - hy, hy},
{θ, -Pi, Pi - hθ, hθ}
];
]
gives (vectors shown only at one point)

The problem is that the calculation is slow. The desired discrete point should be 200 per axis. I think the time is mostly consumed by derivative evaluation. So how could I speed up the calculation ? One choice is to transform the code into C++. However the surface function might come from interpolation. I want to stay in Mathematica.

D[f[x, y], {{x, y}}]– J. M.'s missing motivation May 26 '15 at 04:04