mbverifystr-threshold.sql

application/octet-stream

Filename: mbverifystr-threshold.sql
Type: application/octet-stream
Part: 0
Message: Re: speed up verifying UTF-8
-- To use, save http://kermitproject.org/utf8.html as
-- '/tmp/UTF-8 Sampler.html' for test data.
--

-- Call convert_from() to perform UTF-8 verification on input data.
-- Repeat 5 times, return the time elapsed in ms of the fastest run.
create or replace function time_verifystr_threshold(testdata bytea)
returns double precision language plpgsql as $$
declare
  begin_ts timestamptz;
  end_ts timestamptz;

  diff interval;
  min_diff interval;

begin
  for i in 1..10 loop
    begin_ts := clock_timestamp();

    perform bool_or(convert_from(testdata, 'utf8') = 'x') from generate_series(1, 1000000);

    diff := clock_timestamp() - begin_ts;

    if diff < min_diff or min_diff is null then
       min_diff = diff;
    end if;
  end loop;

  return extract(milliseconds from min_diff);
end;
$$;

select 
round(time_verifystr_threshold(repeat('0123456789abcdef', 1)::bytea)) as asc16,
round(time_verifystr_threshold(repeat('0123456789abcdef', 2)::bytea)) as asc32,
round(time_verifystr_threshold(repeat('0123456789abcdef', 4)::bytea)) as asc64,
round(time_verifystr_threshold(repeat('๐Ÿ’๐Ÿ‘Œ๐ŸŽ๐Ÿ˜', 1)::bytea)) as mb16,
round(time_verifystr_threshold(repeat('๐Ÿ’๐Ÿ‘Œ๐ŸŽ๐Ÿ˜', 2)::bytea)) as mb32,
round(time_verifystr_threshold(repeat('๐Ÿ’๐Ÿ‘Œ๐ŸŽ๐Ÿ˜', 4)::bytea)) as mb64;