How does V2 implement this V1 code?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

How does V2 implement this V1 code?

25 Jan 2020, 12:37

It may seem simple, but I didn't expect that the latest V2 would be so difficult to implement it.

Code: Select all

f:=[]
loop 1000
{
t:=random(1,100)
if f[t]
f[t]++
else if t>f.maxindex() or  t<f.minindex()
f[t]:=1
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How does V2 implement this V1 code?

25 Jan 2020, 13:05

Set the length of the array, use has() and save max and min index yourself. If the length required is too large, use a map.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

25 Jan 2020, 13:08

Helgef wrote:
25 Jan 2020, 13:05
Set the length of the array, use has() and save max and min index yourself. If the length required is too large, use a map.
Yes, it can be done this way. But its performance and simplicity is a disaster.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How does V2 implement this V1 code?

25 Jan 2020, 13:34

Maybe something like this then,

Code: Select all

f:=c.new()
loop 1000
	f[random(1,100)]++


class c extends map {
	__item[i] => this.has(i) ? base[i] : 0
}
Edit, simplified.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

25 Jan 2020, 13:42

@Helgef The point is "min-max" ...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How does V2 implement this V1 code?

25 Jan 2020, 14:25

Sorry, I had my mind elsewhere. Regarding the original suggestion, is it really so bad?

Code: Select all

f:=map()

min := 100
max := 1
loop 1000
{
t:=random(1,100)
if f.has(t)
	f[t]++, 
else if (t>max && max:=t) or  (t<min && min:=t)
	f[t]:=1
}
Edit, well, this isn't equivalent of min/maxindex().
edit2,

Code: Select all

f:=map()

min := 100
max := 0
loop 1000
{
t:=random(1,100)
if f.has(t)
	f[t]++, 
else if t>max or  t<min
	f[t]:=1, max:=max(t,max), min:=min(t,min)
}
Last edited by Helgef on 25 Jan 2020, 14:40, edited 1 time in total.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

25 Jan 2020, 14:37

@Helgef
The situation may be more complicated. "F" will be passed multiple times as a parameter, it cannot carry the two variables "min"-"max".

So my current method is:

Code: Select all

max(f){
for i in f
max:=i
return max
}
min(f){
for i in f
return i
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How does V2 implement this V1 code?

25 Jan 2020, 14:53

I see, maybe this then :lol:

Code: Select all

f:=c.new(100)
loop 1000
{
t:=random(1,100)
if f.has(t)
f[t]++
else if t>f.max or  t<f.min
f[t]:=1
}

class c extends map {
	__item[i] {
		set => (this.minmax(i), base[i] := value)
	}
	max := 0
	__new(max) => this.min := max
	minmax(i) {
		this.max:=max(i,this.max), this.min:=min(i,this.min)
	}
}
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

25 Jan 2020, 15:05

@Helgef

Yes, but compared to V1, its performance and simplicity are desperate.
So I have been thinking, what did V2 gain in exchange for the lost flexibility and simplicity? :(
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: How does V2 implement this V1 code?

25 Jan 2020, 17:59

HotKeyIt wrote:
25 Jan 2020, 17:39
I have added MinIndex and MaxIndex to Map, not sure why lexikos did not take them over
probably because Maps are supposed to be associative array dictionary types, and therefore its weird to query min/max integer keys when you are expecting random key names

the OP still hasn't yet fully described his use case. perhaps his design and data type choices are bad

User avatar
lvalkov
Posts: 58
Joined: 08 Mar 2019, 16:39

Re: How does V2 implement this V1 code?

25 Jan 2020, 18:00

You speak of "increased complexity", "loss of flexibility" and "lackluster performance" when in reality you couldn't be further from the truth.
  • The last solution posted by user @Helgef executes about half an order of magnitude faster than the stock v1 sample. (See edit below.)
  • Flexibility would have been impacted had there not existed a way of replicating in v2 what you had originally asked for.
  • The degree of complexity can be debated. I'd be hard-pressed to call a 10-LoC implementation "complex". Suffice it to say, if one seeks to only get by merely relying on old v1 paradigms instead of making oneself familiar with the new ones, a great majority concepts will seem complicated.
Last edited by lvalkov on 26 Jan 2020, 09:27, edited 1 time in total.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

25 Jan 2020, 21:16

@Helgef @HotKeyIt Thank you.


@lvalkov
Regarding performance, my test results are the opposite of yours.
loop 1000000
V2 V100 0.550465S
V2 V108 0.903593S

I can't imagine why you think this function will be faster than the built-in functions.
minmax(i) {
this.max:=max(i,this.max), this.min:=min(i,this.min)
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How does V2 implement this V1 code?

26 Jan 2020, 03:15

@lvalkov, hi, I'd expect my example to be slower due to minmax() method as pointed out, perhaps you forgot setbatchlines -1 when testing in v1.
@arcticir, I understand that it might be inconvenient when built in functionality is removed, but really, min/maxindex() makes no sense for either array or map types, imo. You might have an edge case where you really need it, but to conclude from this that v2 has lost flexibility and simplicity seems a bit hasty. The new object implementation makes it trivial to extend built-in functionality. Consider how you'd add min/maxindex() methods in v1 if they weren't present.

Just as yet another alternative for you,

Code: Select all

f:=map()
min := 100
max := 0

loop 1000
{
t:=random(1,100)
if f.has(t)
	f[t]++, 
else if t>max or  t<min
	f[t]:=1, max:=max(t,max), min:=min(t,min)
}
f.min := min
f.max := max
this is simple and straightforward, and doesn't require you to carry around the min/max variables, but you still have to update the min/max properties if f is modified elsewhere.

Cheers.
arcticir
Posts: 694
Joined: 17 Nov 2013, 11:32

Re: How does V2 implement this V1 code?

26 Jan 2020, 05:15

Thank you This brings me to a new understanding of built-in functions, which are not necessarily faster than custom functions.
User avatar
lvalkov
Posts: 58
Joined: 08 Mar 2019, 16:39

Re: How does V2 implement this V1 code?

26 Jan 2020, 09:23

Helgef wrote:
26 Jan 2020, 03:15
perhaps you forgot setbatchlines -1 when testing in v1.
No. I had simply copied @arcticir's snippet and executed it with the 1.1.32.00 interpreter ("implement this V1 code" implying v1). I failed to account for the fact that the piece of code was, indeed, not v1-compliant (t:=random(1,100)). Unfortunately, the interpreter (through no fault of its own), too, failed to alert me. What had happened is that Random.ahk, part of a library I had been testing these past couple of days, was auto-included from my AutoHotkey Lib folder. The function performs extra checks in addition to having other user-defined functions as dependencies, hence the atrocious performance.

Having now benchmarked the sample again with the proper built-in command (Random t, 1, 100), I can see that it is on average about twice as fast as @Helgef's v2 solution.

Sorry for the misinformation, @arcticir.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How does V2 implement this V1 code?

26 Jan 2020, 11:06

guest3456 wrote:
25 Jan 2020, 17:59
HotKeyIt wrote:... not sure why lexikos did not take them over
probably because Maps are supposed to be associative array dictionary types, and therefore its weird to query min/max integer keys when you are expecting random key names
Ok, but it would be then also weird to build a class with functions that extends Map or Array, right?
I think it is good to have usefull methods build-in.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: How does V2 implement this V1 code?

26 Jan 2020, 11:26

HotKeyIt wrote:
26 Jan 2020, 11:06
Ok, but it would be then also weird to build a class with functions that extends Map or Array, right?
I think it is good to have usefull methods build-in.
i think the idea is what Helgef said: Min and MaxIndex are not very useful most of the time, but if you need them for very specific edge cases, then you can easily extend the class to provide them yourself, as he showed. normal arrays start at 1 and you keep pushing new items, so maxIndex = .Length, thats the most common use case

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: How does V2 implement this V1 code?

07 Feb 2020, 09:45

Hoping this is still on topic...

Is there an equivelant to has() for plain objects? Or would I need to use something like Try/Catch?

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: ntepa and 29 guests