Which function for creating a POINT in PostGIS?
My guess is that ST_MakePoint
is fastest, but this is easy enough to benchmark with 100k random points.
\timing
WITH test AS (
SELECT <POINT CONSTRUCTOR METHOD>
FROM generate_series(1,100000)
)
SELECT count(*) FROM test;
And here are some results with PostGIS 2.1 (trunk) on PostgreSQL 9.1, x64 Debian. I did them a few times to get an approximate average. Here are the <POINT CONSTRUCTOR METHOD>
in order from fastest to slowest:
ST_SetSRID(ST_MakePoint(random(), random()), 4326)
- avg 160 ms
- by far fastest, and preserves double-point precision (lossless)
- easiest way to make a parameterized query with numeric coordinate data
ST_GeomFromText('POINT(' || random()::text || ' ' || random()::text || ')', 4326)
- avg 760 ms
- slow, as the number is cast to text, then the string is pieced together, then PostGIS needs to parse it to find the numbers
- lossy, due to number -> text -> number conversions
ST_GeomFromEWKT('SRID=4326;POINT(' || random()::text || ' ' || random()::text || ')')
- avg 810 ms
- slowest, not sure why it's slower than
ST_GeomFromText
Lastly, a wee footnote on the difference between lossless/lossy conversions with the above methods. Only ST_MakePoint
preserves the binary floating point precision data, and the text conversions truncate a very small part of the data off. Although the two points may have binary differences (seen in the WKB), they should always be spatially equal. The distance differences are essentially the machine epsilon for double precision.
SELECT
(geom_text = geom_binary) AS spatially_equal,
(geom_text::text = geom_binary::text) AS binary_equal,
(ST_AsText(geom_text) = ST_AsText(geom_binary)) AS wkt_equal,
ST_Distance(geom_text, geom_binary)
FROM (
SELECT x, y,
ST_GeomFromText('POINT(' || x::text || ' ' || y::text || ')') AS geom_text,
ST_MakePoint(x, y) AS geom_binary
FROM (SELECT random()::float8 as x, random()::float8 as y) AS f1
) AS f2;
spatially_equal | binary_equal | wkt_equal | st_distance
-----------------+--------------+-----------+----------------------
t | f | t | 1.38777878078145e-16
ST_MakePoint and ST_Point are the same - they both call LWGEOM_makepoint (you can see this in the postgis/postgis.sql.in file in the source code). I'd use ST_MakePoint. The text conversion routines produce the same result, but are slower because of the amount of parsing that is required.