function [error, irror] = ws_srs_error(spec, xmax, xmin, nspec)
% ws_srs_error (highly optimized MATLAB version)
%
% Computes total and maximum SRS log error between target (spec)
% and response spectra (xmax, xmin).

    %# Faster near-zero detection (no find)
    if any(spec < 1e-20)
        % One-time, optional warning (fast)
        warning('Near-zero values detected in SRS spec (clamped).');
        
        % Clamp only once (vectorized)
        spec = max(spec, 1e-20);
    end

    %# Precompute inverse (much faster than repeated ./)
    inv_spec = 1 ./ spec;

    %# Compute log10 ratios vectorized
    % Combine xmax and xmin into one matrix for faster operations
    ratio = abs(log10([xmax .* inv_spec; xmin .* inv_spec]));

    %# Total error
    error = sum(ratio);

    %# Maximum individual error
    irror = max(ratio);

end
