​push_swap /
src / metrics / bench.c
162 commits
30-04-2026
58 lines 2.2 kB
 1/* ************************************************************************** */
 2/*                                                                            */
 3/*                                                        :::      ::::::::   */
 4/*   bench.c                                            :+:      :+:    :+:   */
 5/*                                                    +:+ +:+         +:+     */
 6/*   By: ohaponiu <marvin@42.fr>                    +#+  +:+       +#+        */
 7/*                                                +#+#+#+#+#+   +#+           */
 8/*   Created: 2026/04/30 08:27:29 by ohaponiu          #+#    #+#             */
 9/*   Updated: 2026/04/30 12:56:59 by ohaponiu         ###   ########.fr       */
10/*                                                                            */
11/* ************************************************************************** */
12
13#include "metrics.h"
14#include <libft.h>
15#include <unistd.h>
16
17static const char	*strategy_label(t_strategy s, int dis)
18{
19	if (s == STRAT_SIMPLE)
20		return ("Simple / O(n\xc2\xb2)");
21	if (s == STRAT_MEDIUM)
22		return ("Medium / O(n\xe2\x88\x9an)");
23	if (s == STRAT_COMPLEX)
24		return ("Complex / O(n log n)");
25	if (dis < 0.2 * DISORDER_RANGE)
26		return ("Adaptive / O(n\xc2\xb2)");
27	if (dis < 0.5 * DISORDER_RANGE)
28		return ("Adaptive / O(n\xe2\x88\x9an)");
29	return ("Adaptive / O(n log n)");
30}
31
32static int	total_ops(const t_ops *ops)
33{
34	return (ops->sa + ops->sb + ops->ss + ops->pa + ops->pb
35		+ ops->ra + ops->rb + ops->rr + ops->rra + ops->rrb + ops->rrr);
36}
37
38void	bench(const t_context *ctx)
39{
40	const t_ops	*ops;
41	t_strategy	s;
42	int			dis;
43
44	ops = &ctx->ops;
45	s = ctx->flags.strategy;
46	dis = ctx->disorder;
47	ft_dprintf(STDERR_FILENO, "[bench] disorder:   %d.%d%d%%\n",
48		dis / 100, (dis % 100) / 10, dis % 10);
49	ft_dprintf(STDERR_FILENO, "[bench] strategy:   %s\n",
50		strategy_label(s, dis));
51	ft_dprintf(STDERR_FILENO, "[bench] total_ops:  %d\n", total_ops(ops));
52	ft_dprintf(STDERR_FILENO,
53		"[bench] sa: %d  sb: %d  ss: %d  pa: %d  pb: %d\n",
54		ops->sa, ops->sb, ops->ss, ops->pa, ops->pb);
55	ft_dprintf(STDERR_FILENO,
56		"[bench] ra: %d  rb: %d  rr: %d  rra: %d  rrb: %d  rrr: %d\n",
57		ops->ra, ops->rb, ops->rr, ops->rra, ops->rrb, ops->rrr);
58}