Two swift functions increasing compiling time dramatically - ios

After going back and reading my app's build logs, there seems to be a strange issue where two (relatively) simple functions are both increasing the compiling time by one minute each (58 & 53 seconds respectively). This can be seen in my build logs below:
These functions are in my CAAgeViewController and both reference a UISlider in my storyboard. They make sure that both the max and min sliders are at most within 1 year of each other, and either function sets a label to "18-24 Years" or something to that respect. They are as follows:
#IBAction func minAgeChanged(_ sender: UISlider) {
if round(minAgeSlider.value / 1) < round(maxAgeSlider.value / 1) - 1 {
minAgeSlider.value = round(minAgeSlider.value / 1)
ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
} else {
minAgeSlider.value = round(maxAgeSlider.value / 1) - 1
ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
}
}
#IBAction func maxAgeChanged(_ sender: UISlider) {
if round(maxAgeSlider.value / 1) > round(minAgeSlider.value / 1) + 1 {
maxAgeSlider.value = round(maxAgeSlider.value / 1)
ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
} else {
maxAgeSlider.value = round(minAgeSlider.value / 1) + 1
ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
}
}
I am unsure what exactly is going wrong here. Any help is appreciated!

Your problem is the chained-plus. (It's always chained-plus; ok, not always but always…)
ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
Replace this with:
ageLabel.text = "\(Int(round(minAgeSlider.value)))-\(Int(round(maxAgeSlider.value))) Years"
I'm fairly certain the /1 isn't helping you here. round + Int should do everything you meant.

The most likely cause of the slowdown is the way that you're concatenating strings with +. For some reason that I haven't been able to figure out, it's much faster (at least in Swift 2/3) to append strings with "\(string1) \(string2)" than to use string1 + string2. Same goes for arrays and their + operator. We saw a 100x decrease in compile time when we changed how we're doing concatenation.

Have you tried removing the division by 1 and casting your values to Double instead? They may already be Doubles, mind you. You may just need to call round().

Related

Can't replicate RStan ESS code from Vehtari paper

I am trying to replicate an ESS (effective sample size) calculation using the method of Vehtari et al. in: Rank-normalization, folding, and localization: An improved Rhat for assessing convergence of MCMC
I am working from the code here:
https://github.com/avehtari/rhat_ess/blob/master/code/monitornew.R
# Geyer's initial positive sequence
rho_hat_t <- rep.int(0, n_samples)
t <- 0
rho_hat_even <- 1
rho_hat_t[t + 1] <- rho_hat_even
rho_hat_odd <- 1 - (mean_var - mean(acov[t + 2, ])) / var_plus # 251
rho_hat_t[t + 2] <- rho_hat_odd
while (t < nrow(acov) - 5 && !is.nan(rho_hat_even + rho_hat_odd) &&
(rho_hat_even + rho_hat_odd > 0)) {
t <- t + 2
rho_hat_even = 1 - (mean_var - mean(acov[t + 1, ])) / var_plus # 256
rho_hat_odd = 1 - (mean_var - mean(acov[t + 2, ])) / var_plus # 257
if ((rho_hat_even + rho_hat_odd) >= 0) {
rho_hat_t[t + 1] <- rho_hat_even
rho_hat_t[t + 2] <- rho_hat_odd
}
}
I can follow the code from the paper except when we get to equation 10 in the paper (calculating the cross-chain autocorrelation). The code (lines 251, 256 and 257) appears in the form:
1 - (mean_var - mean(acov[t + 1, ])) / var_plus
which is close to equation 10, except the missing the 's' terms from equation 10:
I can't see anywhere in the code that this is somehow accounted for elsewhere in the way the calculation is being done. I have tried putting the 's' terms back into those lines of code and it makes a big difference to the final ESS value.
Is anyone able to help me understand the discrepancy between paper and code?
Thanks.
In the formula in the paper, s^2 is is the estimate of variance and rho the estimate of autocorrelation. Thus s^2 * rho is an estimate of the autocovariance, which is what you see in the code.

How to avoid Round-up from NormalizeDouble and OrderSend Pending Order (SELLLIMIT, BUYLIMIT) get Error -1 Invalid stops

Please advice me how to solve problem when OrderSend Pending Order ( BUYLIMIT, SELLLIMIT) got error -1 Invalid stops.
also tell me the rule of BUYLIMIT and SELLLIMIT.
the simple code like below:
`
double digit = MarketInfo(symbol,MODE_DIGITS);
POPRICE = NormalizeDouble(BBMVAL[0],digit);
TPPRICE = NormalizeDouble(POPRICE + (30*point),digit)
SLPRICE = NormalizeDouble(POPRICE - (30*point),digit)
ticket1=OrderSend(symbol,OP_BUYLIMIT,0.1, POPRICE,Slippage,SLPRICE,TPPRICE,BUYLIMIT,magic,(TimeCurrent()+(3600*2)),CLR_NONE);
Sleep(10);
while(IsTradeContextBusy()) Sleep(100);
RefreshRates();
if(ticket1 < 0)
{
SendMail
(
symbol+"-"+ IntegerToString(Period())+"-" + "FAILED-BUYLIMIT ",
symbol+"-"+ IntegerToString(Period())+"-" + "FAILED-BUYLIMIT "+"(#PO-Price:"+POPRICE+"#TP:"+TPPRICE++"#SL:"+SLPRICE+"#RespID:"+ticket1+"#Status: "+ErrorDescription(GetLastError())+")"
);
ticket1 = 0;
}
if (ticket1 > 0)
{
b_Status = 1;
SendMail
(
symbol+"-"+ IntegerToString(Period())+"-" + "SUCCESSED-BUYLIMIT ",
symbol+"-"+ IntegerToString(Period())+"-" + "SUCCESSED-BUYLIMIT "+"(#PO- Price:"+POPRICE+"#TP:"+TPPRICE+"#SL:"+SLPRICE+"#RespID:"+ticket1+"#Status: "+ErrorDescription(GetLastError())+")"
);
ticket1 = 0;
}
the objective is :
If the pair EURUSD 5-digit, How to make the SLPRICE, POPRICE, TPPRICE always with 5-digit. sometimes with 5 digit and sometime roundup with 4-digit. is it right to use NormalizeDouble ? how to avoid the roundup.
when got Failed ( tiket1 < 0 ), the error is -1 ( invalid stops), what is the real reason of this error. sometimes successed and sometime failed. what is the rules of selllimit and the buylimit.
and when got Failed, there are many emails send to email-address. how to avoid this problem too.
THank you very much.
It is perfectly okay to have a 4 digit price when 5 digit decimal is expected, but since double is a floating-point number, sometimes you don't get the exact price you might expect,
for example:
let's say you are calculating TP for a long trade, 1.10000 + 0.0020 = 1.10020(expected) but you might get something like this 1.10020001, in these cases the price level is rejected, causing errors. That is why price normalizing is important.
In some pairs, you might find that NormalizeDouble() sometimes gives false values, in those cases use the following function.
double NP(const double price)
{
double tickSize = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
return (MathRound(price / tickSize) * tickSize );
}
As for getting the many emails: you can limit the number of emails per period of time.
Here I made it so that you get 1 email per new candle:
if(ticket1 < 0)
{
static datetime prevTime = 0;
if(Time[0] > prevTime)
{
SendMail
(
symbol + "-" + IntegerToString(Period()) + "-" + "FAILED-BUYLIMIT ",
symbol + "-" + IntegerToString(Period()) + "-" + "FAILED-BUYLIMIT " + "(#PO-Price:" + POPRICE + "#TP:" + TPPRICE++"#SL:" + SLPRICE + "#RespID:" + ticket1 + "#Status: " + ErrorDescription(GetLastError()) + ")"
);
prevTime = Time[0];
}
ticket1 = 0;
}

Handle Multiple UISlider

I have 4 UISlider with minimum value 0, Maximum value 10. I have one constant value(Ex:20).
My goal is sum of 4 slider value doesn't go above that constant value.
Ex: if i drag and set to maximum value of 1st and 2nd slider(i.e 10), Now the sum of 4 slider is 20.
I don't want to allow other 2 sliders(3rd,4th) to increase. if i decrease any slider(1st or 2nd) value means then i can allow them(2,3,4) to increase sliders values.
help me to achieve this.
You can see this screen shot
You can see this screen shot
In the #IBAction sliderValueChanged, calculate the sum of all sliders. If the sum is greater the the maximum, just reset the value of the "causing" slider (e.g. the one that is currently being dragged by the user) to the maximum allowed value.
// ...
let maxSum = 20
// ...
#IBAction func sliderValueChanged(_ sender: UISlider) {
let sum = slider1.value + slider2.value + slider3.value + slider4.value // better: use outlet collection
if (sum > maxSum) {
let overflow = sum - maxSum
sender.value = sender.value - overflow
}
}
Have a central method that all the sliders call for the valueChanged event.
In that method, sum the values of all the sliders. As the sum changes, adjust the max of all the sliders so that increasing any one slider can't exceed your sum total value. (Each slider's new max would be the the difference between it's current value and max_sum_total - current_sum_total.
So if you set all the sliders to 5, your method would adjust the max for all the sliders to 5.
If Slider A was at 10, Slider B was at 5, and the others were at 0,
Slider A max would be MIN((20 - (A+B+C+D) - A), 10) //5
Slider B max would be MIN((20 - (A+B+C+D) - B), 10) //10
...and so on.
You can use the below code for rough start. i am using single method to handle all 4 UISliders. you can refactor code as you want
#IBAction func touchDrag(_ sender: UISlider) {
switch sender {
case slider1:
let f1 = (sender.value + slider2.value)
let f2 = (slider3.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider2.value + slider3.value + slider4.value)
}
case slider2:
let f1 = (slider1.value + sender.value)
let f2 = (slider3.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider3.value + slider4.value)
}
case slider3:
let f1 = (slider1.value + slider2.value)
let f2 = (sender.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider2.value + slider4.value)
}
default:
let f1 = (slider1.value + slider2.value)
let f2 = (slider3.value + sender.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider2.value + slider3.value)
}
}
}

Need to restart numbering when it gets to 1 and jump to 99

How would I go about doing this? the code I have is
let myInt = Int(currentSaleLabel.text!)
let sale1Number = (myInt! - 1 + 100) % 100
let sale2Number = (sale1Number - 1 + 100) % 100
let sale3Number = (sale2Number - 1 + 100) % 100
let sale4Number = (sale3Number - 1 + 100) % 100
let sale5Number = (sale4Number - 1 + 100) % 100
let sale6Number = (sale5Number - 1 + 100) % 100
let sale7Number = (sale6Number - 1 + 100) % 100
let sale8Number = (sale7Number - 1 + 100) % 100
let sale9Number = (sale8Number - 1 + 100) % 100
let sale10Number = (sale9Number - 1 + 100) % 100
This works fine for numbers 0-99 but I need to eliminate the number 0 also.
One approach is to make a limit of 99 with modulo % operator, like this:
let sale1Number = (myInt! - 1 + 100) % 100
This decrements myInt, and wraps it to 99 when it gets negative. Adding 100 prior to applying % has no effect on numbers in range 0..99. Negative numbers above -100 will get processed correctly.
I am actually also trying to avoid the number 0
You can use a modified formula, like this:
let sale1Number = (myInt! - 2 + 100) % 100 + 1

print multicolumn formatted text vb6

i need to print formatted text like in the image below, how can i achive this in vb6, given that vb6 print object is not friendly for such this
The data i need to print that represented by the boxes are non related
It is not very difficult. You use the ScaleLeft, ScaleWidth, CurrentX, and CurrentY properties to set where printing begins on the page. In this case you will probably also want to set the Orientation property to vbPROPortrait. Using those positioning properties, and setting the font and style you want you then call Printer.Print
This method will draw 4 boxes onto a page. Play with the (x, y) coordinates or hard code the numbers to alter the sizes. Remove the .EndDoc statement if you don't want the printer to print the page from this method and call Printer.EndDoc from somewhere else. Full Printer object documentation for VB6 can be found at http://msdn.microsoft.com/en-us/library/aa443915%28v=vs.60%29.aspx
Private Sub DrawBox()
With Printer
.ScaleMode = vbTwips
lngScaleWidth = .ScaleWidth
lngScaleHeight = .ScaleHeight
Printer.Line (.ScaleLeft + lngMargin, .ScaleTop + lngMargin)-(lngScaleWidth / 2 - (100 + lngMargin * 2), lngScaleHeight / 2 - (100 + lngMargin * 2)), lngColor, B
Printer.Line (lngScaleWidth / 2 + (100 + lngMargin * 2), .ScaleTop + lngMargin)-(.ScaleWidth - lngMargin, lngScaleHeight / 2 - (100 + lngMargin * 2)), lngColor, B
Printer.Line (.ScaleLeft + lngMargin, lngScaleHeight / 2 + (100 + lngMargin * 2))-(lngScaleWidth / 2 - (100 + lngMargin * 2), .ScaleHeight - lngMargin), lngColor, B
Printer.Line (lngScaleWidth / 2 + (100 + lngMargin * 2), lngScaleHeight / 2 + (100 + lngMargin * 2))-(.ScaleWidth - lngMargin, .ScaleHeight - lngMargin), lngColor, B
.EndDoc
End With
End Sub
The sample code below demonstrates some of the positioning and other properties.
Dim lMargin as Integer
lMargin = 200
With Printer
.FontBold = True
.FontItalic = False
.CurrentY = .CurrentY + (3 * .TextHeight(App.ProductName))
.CurrentX = lLeftMargin
.FontName = "Arial"
.FontSize = 11
Printer.Print "Date " & strTransDate
End With

Resources