Commit cb8822d8 authored by roset.alwzrah's avatar roset.alwzrah

Find the number of occurrences of a number in an array

parents
Pipeline #178 canceled with stages
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="bd82bf16-4018-4075-b7d4-6d792f3cd58b" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Service/NumOfOccurrences.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/NumOfOccurrencesTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2NKl3Ma6JBhu4WPrU4yJSnzE8yx" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="bd82bf16-4018-4075-b7d4-6d792f3cd58b" name="Changes" comment="" />
<created>1679420459839</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1679420459839</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>rosette_alwazzeh</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package Service;
import java.util.Arrays;
import java.util.concurrent.RecursiveAction;
public class NumOfOccurrences extends RecursiveAction{
public long occur=0;
int arr[], num,lo, hi;
public NumOfOccurrences(int[] arr, int num, int lo, int hi) {
this.arr = arr;
this.num = num;
this.lo=lo;
this.hi=hi;
}
public long computeSeq() {
for (int i = lo; i <= hi; ++i) {
if (arr[i]==num)
occur++;
}
return occur;
}
@Override
protected void compute() {
if (hi - lo > 125000000) {
int mid = (lo + hi) / 2;
NumOfOccurrences left = new NumOfOccurrences (arr,num, lo, mid);
NumOfOccurrences right = new NumOfOccurrences (arr,num ,mid + 1, hi);
left.fork();
right.compute();
left.join();
occur = left.occur + right.occur;
} else {
occur =computeSeq();
}
}
public void computeParallelStream() {
occur = Arrays.stream(arr).asLongStream().parallel().filter(a->a==num).count();
}
public void computeStream() {
occur = Arrays.stream(arr).asLongStream().filter(a->a==num).count();
}
}
import Service.NumOfOccurrences;
import junit.framework.TestCase;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
public class NumOfOccurrencesTest extends TestCase {
Random rand = new Random();
int num=919;
int size = 1000_000_000;
private int[] randomArray(int size) {
long s = 115;
rand.setSeed(s);
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (rand.nextInt(100000)+1);
}
return arr;
}
public void testNumOfOccurrencesSeq() {
int[] arr = randomArray(size);
NumOfOccurrences array = new NumOfOccurrences(arr, num,0, arr.length - 1);
long start = System.currentTimeMillis();
long occur = array.computeSeq();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Sequential Time execution for Random Array of size %d is %d ms,the number of occurrences of %d in Array is %d\n", size, endTimer, num,
occur);
}
public void testNumOfOccurrencesPP() {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
int[] arr = randomArray(size);
NumOfOccurrences array = new NumOfOccurrences(arr, num,0, arr.length - 1);
long start = System.currentTimeMillis();
ForkJoinPool.commonPool().invoke(array);
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Parallel Time execution for Random Array of size %d is %d ms, the number of occurrences of %d in Array is %d\n", size, endTimer, num,
array.occur);
}
public void testComputeStream(){
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
int[] arr = randomArray(size);
NumOfOccurrences array = new NumOfOccurrences(arr, num,0, arr.length - 1);
long start = System.currentTimeMillis();
array.computeStream();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Sequential Stream Time execution for Random Array of size %d is %d ms, the number of occurrences of %d in Array is %d\n", size, endTimer, num,
array.occur);
}
public void testComputeParallelStream(){
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","7");
int[] arr = randomArray(size);
NumOfOccurrences array = new NumOfOccurrences(arr, num,0, arr.length - 1);
long start = System.currentTimeMillis();
array.computeParallelStream();
long endTimer = System.currentTimeMillis() - start;
System.out.printf("Parallel Stream Time execution for Random Array of size %d is %d ms, the number of occurrences of %d in Array is %d\n", size, endTimer, num,
array.occur);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment