我刚刚开始学习 Java 中的 Streams 和并行,我想知道为什么普通的 for 循环IntStream在向数组添加项时比并行花费的时间更少。
package parallel;
import java.util.stream.IntStream;
public class Parallel {
public static void main(String[] args) {
final int[] intArray = new int[100000];
long startTime = System.currentTimeMillis();
IntStream.range(0, 100000).parallel().forEach(i -> intArray[i]=i);
long endTime = System.currentTimeMillis();
System.out.println("Parallel time: " + (endTime-startTime));
final int[] intArray2 = new int[100000];
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++){
intArray2[i] = …Run Code Online (Sandbox Code Playgroud) ../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c
Building target: NoteConverter
Invoking: GCC C Linker
gcc -o "NoteConverter" ./NoteConverter.o
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1
Run Code Online (Sandbox Code Playgroud)
以下是代码
/**
* Frequency octave finder and play note
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#define DURATION 5
#define SAMPLERATE 48000
#define AMPLITUDE 1
//A*sin(2*pi*n*f/R)
double yCord(int n, double freq)
{
double y= sin(2*M_PI*freq*n/48000); …Run Code Online (Sandbox Code Playgroud) 我不知道我在哪里出错了,任何帮助都会有所帮助.我试图用2个不同的字符串数组制作一副牌并将其打印到控制台.编译很好,但是当我运行它时,我得到"Segmentation fault(core dumped)"
/*
* BlackJack.c
*
* Created on: Feb 25, 2014
* Author: Danny Hunn
* 25 Feb 14 builds a deck for Black Jack
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define NUM_SUITS 4
#define DECK_SIZE 52
#define NUM_RANKS 13
void swap(char *first,char *second)// swapping pointers
{
char temp = *first;
*first = *second;
*second = temp;
}
void shuffle(char *deck[])
{
int seed, gen, i;
seed = (int) time(0);
srand(seed);
for(i =0; i < …Run Code Online (Sandbox Code Playgroud)