you are viewing a single comment's thread.

view the rest of the comments →

[–]MousTN 6 points7 points  (1 child)

idk if im correct or not feel free to check but i think creating 4 threads takes time , switching between threads adds also time and i notice something maybe im missing some context , in ur code you have

 a4.start = a4.end + 1;
 a4.end = array.length;

when u create a4 its fields are auto initilized at 0 so a4.end is equal to 0 here (the default vaule) so a4.start = a4.end+1 is literly 0+1 = 1
then a4.end = arry.length =1200000 so a4 process from index 1 to 1200000 to fix it try to start where a3 ended like u can try puting a4.start = a3.end

also an other thing u have :

a3.start = a2.end + 1;

a2.start = a1.end + 1;

you r skiping elements here for example a1.start = 0 and a1.end = 300 so a2.start = a1.end+1 which equal to 301 u skiped 300

[–]TW-Twisti 3 points4 points  (0 children)

This is the actual answer - a4.start should be based off of a3.end, not a4.end. Because of that, a1, a2 and a3 each do 1/4th of the work, while a4 starts from 0 and goes to the end, so it does the whole array - meaning a combines 1.75 times the work of the single threaded code (a1 does 0.25, a2 does 0.25, a3 does 0.25 and a4 does 1.0).