Question: Consider some region in 3D space. Example is

```
reg = Cylinder[{{0, 24, 14}, {0, 42.7, 14}}, 9]

```

I want to find the minimal and maximal polar angle $\theta = \arccos\left(\frac{z}{r}\right)$ covered by this region. A brute-force way would be just to generate random points belonging to the region, convert to $\theta$, and find min/max:

```
points = {ArcCos[#[[3]]/Sqrt[#[[1]]^2 + #[[2]]^2 + #[[3]]^2]]} & /@ 
  Table[RandomPoint[reg], 10^4]//Flatten;
thmin=Min[points]
thmax=Max[points]

```

> 
> 0.81
> 
> 
> 

> 
> 1.44
> 
> 
> 

However, the method is slow: accurate enough results (say, 3 digits after the comma) would require a large number of generated points. In addition, it would be much slower for more complicated figures. Is there any other way to evaluate the minimal and maximal angles?

P.S. The brute method may be made a bit more efficient if generating points belonging to `RegionBoundary[reg]`. However, I have found that Mathematica gets stuck in this case if `reg` is parallelepiped.

Answer: 
Running on an M1 Max (essentially an 8 core machine). Not only does `Compile` compile faster than `FunctionCompile`; it also allows parallelization via OpenMP:

```
f = FunctionCompile[Function[
     {
      Typed[list1, "PackedArray"::["Integer64", 1]],
      Typed[list2, "PackedArray"::["Integer64", 1]]
      },
     Table[i - list2, {i, list1}]],
    UseEmbeddedLibrary -> True
    ]; // AbsoluteTiming

```

> 
> {1.6023, Null}
> 
> 
> 

```
cf = Compile[{{a, _Integer}, {b, _Integer, 1}},
    Table[a - Compile`GetElement[b, j], {j, 1, Length[b]}],
    CompilationTarget -> "C",
    RuntimeAttributes -> {Listable},
    Parallelization -> True,
    RuntimeOptions -> "Speed"
    ]; // AbsoluteTiming

```

> 
> {0.306628, Null}
> 
> 
> 

```
m = 20000;
n = 10000;
a = RandomInteger[{1, 10}, m];
b = RandomInteger[{1, 10}, n];

A = f[a, b]; // RepeatedTiming
B = cf[a, b]; // RepeatedTiming

A == B

```

> 
> {0.225791, Null}
> 
> 
> {0.0697286, Null}
> 
> 
> True
> 
> 
> 

Alas, you need to have a C compiler installed on your system.
**Using `Outer`:**

```
alist = RandomInteger[{1, 10}, 10];
blist = RandomInteger[{1, 10}, 4];

{alist, blist}

```

> 
> {{2, 5, 1, 8, 1, 1, 9, 7, 1, 5}, {2, 9, 6, 2}}
> 
> 
> 

```
(res = Outer[(#1 - #2 &), alist, blist]) // MatrixForm

```

$$\left(
\begin{array}{cccc}
 0 & -7 & -4 & 0 \\
 3 & -4 & -1 & 3 \\
 -1 & -8 & -5 & -1 \\
 6 & -1 & 2 & 6 \\
 -1 & -8 & -5 & -1 \\
 -1 & -8 & -5 & -1 \\
 7 & 0 & 3 & 7 \\
 5 & -2 & 1 & 5 \\
 -1 & -8 & -5 & -1 \\
 3 & -4 & -1 & 3 \\
\end{array}
\right)$$