Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
third_party
skia
tools
Stats.h
Go to the documentation of this file.
1
/*
2
* Copyright 2015 Google Inc.
3
*
4
* Use of this source code is governed by a BSD-style license that can be
5
* found in the LICENSE file.
6
*/
7
8
#ifndef Stats_DEFINED
9
#define Stats_DEFINED
10
11
#include <algorithm>
12
#include <vector>
13
14
#include "
include/core/SkString.h
"
15
#include "
include/private/base/SkFloatingPoint.h
"
16
17
#ifdef SK_BUILD_FOR_WIN
18
static
const
char
*
kBars
[] = {
"."
,
"o"
,
"O"
};
19
#else
20
static
const
char
*
kBars
[] = {
"▁"
,
"▂"
,
"▃"
,
"▄"
,
"▅"
,
"▆"
,
"▇"
,
"█"
};
21
#endif
22
23
struct
Stats
{
24
Stats
(
const
skia_private::TArray<double>
& samples,
bool
want_plot) {
25
int
n = samples.
size
();
26
if
(!n) {
27
min
=
max
=
mean
=
var
=
median
= 0;
28
return
;
29
}
30
31
min
= samples[0];
32
max
= samples[0];
33
for
(
int
i = 0; i < n; i++) {
34
if
(samples[i] <
min
) {
min
= samples[i]; }
35
if
(samples[i] >
max
) {
max
= samples[i]; }
36
}
37
38
double
sum = 0.0;
39
for
(
int
i = 0 ; i < n; i++) {
40
sum += samples[i];
41
}
42
mean
= sum / n;
43
44
double
err = 0.0;
45
for
(
int
i = 0 ; i < n; i++) {
46
err += (samples[i] -
mean
) * (samples[i] -
mean
);
47
}
48
var
=
sk_ieee_double_divide
(err, n-1);
49
50
std::vector<double> sorted(samples.
begin
(), samples.
end
());
51
std::sort(sorted.begin(), sorted.end());
52
median
= sorted[n/2];
53
54
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
55
for
(
int
i = 0; want_plot && i < n; i++) {
56
if
(
min
==
max
) {
57
// All samples are the same value. Don't divide by zero.
58
plot
.
append
(
kBars
[0]);
59
continue
;
60
}
61
62
double
s
= samples[i];
63
s
-=
min
;
64
s
/= (
max
-
min
);
65
s
*= (std::size(
kBars
) - 1);
66
const
size_t
bar = (size_t)(
s
+ 0.5);
67
SkASSERT_RELEASE
(bar < std::size(
kBars
));
68
plot
.
append
(
kBars
[bar]);
69
}
70
}
71
72
double
min
;
73
double
max
;
74
double
mean
;
// Estimate of population mean.
75
double
var
;
// Estimate of population variance.
76
double
median
;
77
SkString
plot
;
// A single-line bar chart (_not_ histogram) of the samples.
78
};
79
80
#endif
//Stats_DEFINED
SkASSERT_RELEASE
#define SkASSERT_RELEASE(cond)
Definition
SkAssert.h:100
SkFloatingPoint.h
sk_ieee_double_divide
static constexpr double sk_ieee_double_divide(double numer, double denom)
Definition
SkFloatingPoint.h:150
SkString.h
kBars
static const char * kBars[]
Definition
Stats.h:20
SkString
Definition
SkString.h:118
SkString::append
void append(const char text[])
Definition
SkString.h:203
skia_private::TArray
Definition
SkTArray.h:40
skia_private::TArray::end
T * end()
Definition
SkTArray.h:402
skia_private::TArray::size
int size() const
Definition
SkTArray.h:416
skia_private::TArray::begin
T * begin()
Definition
SkTArray.h:393
s
struct MyStruct s
Stats
Definition
bilerp-study.cpp:15
Stats::var
double var
Definition
Stats.h:75
Stats::max
double max
Definition
Stats.h:73
Stats::min
double min
Definition
Stats.h:72
Stats::mean
double mean
Definition
Stats.h:74
Stats::plot
SkString plot
Definition
Stats.h:77
Stats::median
double median
Definition
Stats.h:76
Stats::Stats
Stats(const skia_private::TArray< double > &samples, bool want_plot)
Definition
Stats.h:24
Generated on Fri Apr 26 2024 06:18:13 for Flutter Engine by
1.9.8