Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/madcoda/primecounter
Concurrently counting prime numbers
https://github.com/madcoda/primecounter
Last synced: about 1 month ago
JSON representation
Concurrently counting prime numbers
- Host: GitHub
- URL: https://github.com/madcoda/primecounter
- Owner: madcoda
- Created: 2013-10-11T19:05:48.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-10-11T19:06:24.000Z (over 11 years ago)
- Last Synced: 2024-10-20T00:59:43.389Z (3 months ago)
- Language: Java
- Size: 105 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## Prime Counter
This piece of code compute the number of primes within N. i.e. [0 ... N]. This code is multi-threaded. It uses the most basic Divide-and-conquer technique to split the task with equal portion of range of numbers for each worker thread to compute.
Well...this code is not the most elegant nor efficient code, I think it might be useful for beginners to learn how to make use of multi-core CPU, and increase performance of computation. You can also use this code for benchmarking multi-core computers.
### Usage
Download the PrimeCounter.jar
java -jar PrimeCounter.jar [N] [num of threads]
e.g.
java -jar PrimeCounter.jar 999999 4
java -jar PrimeCounter.jar 9999999 16### Building
Using Ant, you can build the project by running "ant"> ant
Buildfile: /home/jason/git/primecounter/build.xmlinit:
[mkdir] Created dir: /home/jason/git/primecounter/bincompile:
[javac] Compiling 2 source files to /home/jason/git/primecounter/bindist:
[mkdir] Created dir: /home/jason/git/primecounter/dist/lib
[jar] Building jar: /home/jason/git/primecounter/dist/lib/PrimeCounter.jarBUILD SUCCESSFUL
Total time: 0 seconds### Observations
On a multi-core computer, increasing the number of threads does shorten the time of computation. If you keep adding more threads, to a certain point it won't get any faster. It is because the CPU can only run limited num of threads concurrently. For example, a Intel i5 processor has 4 cores, with Hyper-threading technology, it can run 8 threads concurrently. (Interestingly, if you increase the number of threads to 16 or higher, you still get some performance increase even though your CPU won't be able to run all of them at once, I still don't know why)