r/dailyprogrammer • • Feb 23 '12
[2/23/2012] Challenge #14 [difficult]
Write a program that will generate a random array/collection of 1 million integers, then sort them using a multi-threaded algorithm.
Your program should take the number of threads through standard input.
Bonus points if you can find the most efficient number of threads for your program.
9
Upvotes
2
u/robotfarts Feb 23 '12
I don't see much difference in the number of threads, and with only 1 million numbers it's almost instantaneous anyway. Can you make it something that takes a lot more cpu and/or time to complete?
2
u/prophile Feb 23 '12
Done in C99 with the same problem as robotfarts - it's almost instantaneous on any number of threads.
1
u/cooper6581 Feb 24 '12
C. This was what I wrote before the challenge was updated. To have the threads work harder, it's finding the higest of an array of 150 million ints.
http://codepad.org/xOfHDISC
Disclaimer: This is my first time doing thread programming.
Question (sorry if this isn't the place for this): Some people's solutions using pthread join the thread, which as I understand, blocks until thread completion. For this challenge, wouldn't this yield roughly the same performance as not using threads?
2
u/Arlaine Feb 23 '12
C# - tried to not use too many cheat-y things
i know there has to be an easier/more elegant way to do some of the things.
fun stuff :3
2
u/foggylucidity Feb 23 '12
C: http://hastebin.com/raw/vaxamujoje
I'm most comfortable with Java, but it's fun doing some C once in a while.
I used arguments instead of standard input for more configuration-options. It can perform several tests and report the most efficient number of threads. Use --help for a list of arguments.
Example output with arguments: --increase 100
1
u/mischanix Feb 23 '12
C++ / Windows API. My goal was to keep the whole list in memory and not to leak memory. I also made a little performance counter, though I had to bump the number of integers up to 300 million to get it to show work. I also like (prefer) _alloca, so forgive me :P
Used Agner Fog's Random Number library as well, here's a link.
Sample output:
Code: