function [hl, area] = perf_profile(H,gate,logplot)

[nf,np,ns] = size(H); % Grab the dimensions

% Produce a suitable history array with sorted entries:
for j = 1:ns
    for i = 2:nf
      H(i,:,j) = min(H(i,:,j),H(i-1,:,j));
    end
end

prob_min = min(min(H),[],3);   % The global minimum for each problem

prob_max = H(1,:,1);           % The starting value for each problem
%prob_max = max(max(H),[],3);


% For each problem and solver, determine the number of evaluations
% required to reach the cutoff value
T = zeros(np,ns);
Nfal = zeros(ns);
%disp(' ');
%disp('------------------------------------------------');
for p = 1:np
  cutoff = prob_min(p) + gate*(prob_max(p) - prob_min(p));
  
  for s = 1:ns
    nfevs = find(H(:,p,s) <= cutoff,1);
    if (isempty(nfevs))
      T(p,s) = NaN;
      Nfal(s) = Nfal(s)+1;
    else
      T(p,s) = nfevs;
    end
    %disp(['cutoff= ' num2str(cutoff) ' gate= ' num2str(gate) ' nfevs= ' num2str(T(p,s)) ' solver= ' num2str(s)]);
    %fprintf('cutoff= %15.6e gate= %15.6e nfevs= %6d solver= %2d\n',cutoff,gate,T(p,s),s);
  end
end
Nfal;
% Other colors, lines, and markers are easily possible:
colors  = ['b' 'r' 'k' 'm' 'c' 'g' 'y'];   lines   = {'-' '-.' '--'};
markers = [ 's' 'o' '^' 'v' 'p' '<' 'x' 'h' '+' 'd' '*' '<' ];

%colors  = ['k' 'k' 'k' 'k' 'k' 'k' 'k'];   lines   = {'-' '-.' '--'};
%markers = [ '' '' '' '' '' '' '' '' '' '' '' ];

if (nargin < 3); logplot = 0; end

% Compute ratios and divide by smallest element in each row.
r = T./repmat(min(T,[],2),1,ns);

% Replace all NaN's with twice the max_ratio and sort.
max_ratio = max(max(r));
r(isnan(r)) = 2*max_ratio;
r = sort(r);

% Plot stair graphs with markers.
hl = zeros(ns,1);
for s = 1:ns
    [xs,ys] = stairs(r(:,s),(1:np)/np);

    % Only plot one marker at the intercept
    %if (xs(1)==1)
    %    vv = find(xs==1,1,'last');
    %    xs = xs(vv:end);   ys = ys(vv:end);
    %end

    sl = mod(s-1,3) + 1; sc = mod(s-1,7) + 1; sm = mod(s-1,12) + 1;
    option1 = [char(lines(sl)) colors(sc) ];
    if (logplot)
        if(s==ns)
            hl(s) = semilogx(xs,ys,option1,'LineWidth',3);
        else
            hl(s) = semilogx(xs,ys,option1,'LineWidth',2);
        end
    else
        if(s==ns)
            hl(s) = plot(xs,ys,option1,'Linewidth',3);
        else
            hl(s) = plot(xs,ys,option1,'Linewidth',2);
        end
    end
    ind = find(xs > 1.1*max_ratio);
    xs(ind) = [];
    ys(ind) = [];
    area(s) = mytrapz(xs,ys);
    if(xs(end) < 1.1*max_ratio)
        area(s) = area(s) + (1.1*max_ratio-xs(end))*ys(end);
    end
    hold on;
end

% Axis properties are set so that failures are not shown, but with the
% max_ratio data points shown. This highlights the "flatline" effect.
if (logplot) 
  axis([1 1.1*max_ratio 0 1]);
  twop = floor(log2(1.1*max_ratio));
  set(gca,'XTick',2.^[0:twop])
else
  axis([1 1.1*max_ratio 0 1]);
end


%legend('DFL', 'NOMAD','Location','SouthEast');
%title(['perf profile \tau = ' num2str(gate)]);
hold off;

end