Optimisation of algorithms. - page 7

 
komposter:

How can you be so sure?

My check shows otherwise:

The mq4 script is in the attachment.

It seems that loop splitting really works faster. But I don't understand why, because it makes two passes.
 
However, it's still not the fastest option. But this is a 'blah blah' on my part, as I won't be writing a fast one.
 
hrenfx:
However, it's still not the fastest option. But it's a 'blah blah' on my part, as I won't be writing a fast one.
Neither will I. Although I agree - it can be faster.
 
hrenfx:
However, it's still not the fastest option. But it's a "blah blah" on my part, as I won't write a fast one.
MetaDriver:
I won't either. I agree though - it could be faster.

Well don't! Wrecked:(

I'll write this algorithm myself, I already know how. Only now I'm sick, I'll have to put it off for a week.

 
TheXpert:
C.T.D. wrote exactly that in his first post.
I'm not claiming primacy, I kinda agreed with the suggestion: "better to really split into 2 cycles" )
 
C-4:
Cycle splitting does seem to work faster. But I don't understand why, as the passes become two.
Because each pass has twice as few checks, and the passes themselves often get shorter (prematurely interrupted).
 
MetaDriver:
And I won't. Although I agree - it can be faster.

I won't either, although faster is definitely possible ;)

I just noticed that one break is missing, so I can't go deep into it now.

 

Here is the final code. A maximum search function is presented. The function for finding minima is similar:

private BitArray Up()
        {
            BitArray bits = new BitArray(MyQuotes.Count);
            double max = double.MinValue;
            int pperiod = (Period - 1) / 2;
            int bar = pperiod;
            int count = MyQuotes.Count - pperiod;
            //последняя позиция второго перебора.
            int pos = bar;
            bool rev = false;
            int rpos = bar;
            bool FirstEnter = false;
            while (bar < count - 1)
            {
                for (int i = 1; i <= pperiod; i++)
                {
                    max = MyQuotes.High[bar - i] > MyQuotes.High[bar + i]
                              ? MyQuotes.High[bar - i]
                              : MyQuotes.High[bar + i];
                    pos = bar + i;
                    if (max > MyQuotes.High[bar])
                    {
                        bar = pos;
                        break;
                    }
                    if (MyQuotes.High[bar + i] == MyQuotes.High[bar] && FirstEnter == false)
                    {
                        rev = true;
                        rpos = bar + i;
                        FirstEnter = true;
                    }
                    if (i == pperiod)
                    {
                        bits[bar] = true;
                        bar = rev ? rpos : pos;
                        rev = false;
                        FirstEnter = false;
                    }
                }
            }
            return bits;
        }

Here are the performance tests:

3       00:00:00.5070290
4       00:00:00.3920224
5       00:00:00.3960227
6       00:00:00.3620207
7       00:00:00.3570204
8       00:00:00.3230185
9       00:00:00.3350192
10      00:00:00.2820161
11      00:00:00.2910166
12      00:00:00.2730157
13      00:00:00.2990171
14      00:00:00.2450140
15      00:00:00.2770158
16      00:00:00.2890165
17      00:00:00.2480142
18      00:00:00.2400138
19      00:00:00.2530144
20      00:00:00.2410138
21      00:00:00.2660152
22      00:00:00.2310132
23      00:00:00.2350135
24      00:00:00.2290131
25      00:00:00.2300131
26      00:00:00.2390137
27      00:00:00.2290131
28      00:00:00.2350135
29      00:00:00.2290131
30      00:00:00.2530144
31      00:00:00.2200126
32      00:00:00.2680153
33      00:00:00.2250129
34      00:00:00.2260129
35      00:00:00.2360135
36      00:00:00.2240128
37      00:00:00.2240128
38      00:00:00.2260129
39      00:00:00.2160124
40      00:00:00.2390137
41      00:00:00.2190125
42      00:00:00.2270130
43      00:00:00.2210126
44      00:00:00.2090120
45      00:00:00.2360135
46      00:00:00.2210126
47      00:00:00.2550146
48      00:00:00.2170124
49      00:00:00.2220127
50      00:00:00.2180124
51      00:00:00.2090120
52      00:00:00.2180125
53      00:00:00.2380136
54      00:00:00.2170124
55      00:00:00.2270130
56      00:00:00.2070118
57      00:00:00.2200126
58      00:00:00.2230128
59      00:00:00.2080119
60      00:00:00.2400137
61      00:00:00.2160123
62      00:00:00.2100120
63      00:00:00.2240128
64      00:00:00.2220127
65      00:00:00.2170124
66      00:00:00.2100120
67      00:00:00.2100121
68      00:00:00.2260129
69      00:00:00.2160123
70      00:00:00.2240128
71      00:00:00.2110121
72      00:00:00.2190125
73      00:00:00.2140123
74      00:00:00.2110121
75      00:00:00.2260129
76      00:00:00.2090119
77      00:00:00.2230128
78      00:00:00.2080119
79      00:00:00.2070118
80      00:00:00.2510144
81      00:00:00.2180125
82      00:00:00.2080119
83      00:00:00.2070118
84      00:00:00.2060118
85      00:00:00.2060118
86      00:00:00.2070118
87      00:00:00.2100120
88      00:00:00.2060118
89      00:00:00.2080119
90      00:00:00.2710155
91      00:00:00.2180125
92      00:00:00.2110120
93      00:00:00.2080119
94      00:00:00.2060118
95      00:00:00.2060118
96      00:00:00.2020116
97      00:00:00.2080119
98      00:00:00.2100120
99      00:00:00.2090119

It can be seen that the processing speed has qualitatively increased and is now independent of the period of the extremum. It is true that for small N, especially for period 3, the speed is even slower, but as N increases, the speed increases rapidly and is almost twice as fast as for small N:

This seems to be due to the fact that break jumps and indexing transitions take some time and are effective over long distances. On small N, head-on brute-forcing turns out to be faster.

P.S. I have put execution of both functions Up() and Down() into asynchronous mode of execution. That is, they can be executed on both cores simultaneously. But it didn't increase performance. Apparently, the passes themselves are not resource-intensive and most of the time is spent on preparation and parsing of data, not on the iterations themselves.

 
hrenfx:
However, it is still not the fastest option. But this is "blah blah" on my part since I'm not going to write a fast one.

Still relevant.

P.S.

C-4:

It can be seen that the processing speed has increased qualitatively and is now independent of the extremum period.

It depends, and not badly. In your case you just have such a source code (CVR) which just ends at minimum N. In general case the graph of dependence of speed of execution on period can be strikingly different from yours.
 
hrenfx:

Still relevant.

P.S.

It depends, and not badly. In your case it is just a source (TSS) which ends at minimum N. In general case the graph of dependence of execution speed on period can drastically differ from yours.

A controversial statement. The algorithm is single-pass and the number of iterations, and hence the speed, is almost independent of N. It is true that there is one peculiarity of the algorithm due to which BP price extremums are equal to each other will cause a significant drop in performance. In all other cases I'm sure the dependence should be preserved. In this case a classical normally distributed random walk was taken as BP.