Is lambda faster than for loop when 1 million pieces of data traverse?

Is lambda faster than for loop when 1 million pieces of data traverse?
Ask online customer service
Scan the code to ask online customer service
  • Number of answers

    ten

  • Number of browses

    1,660

10 answers Default sorting
  • Default sorting
  • Sort by time

The ordinary for loop is the fastest and does not accept refutation

cancel comment

Look at the usage scenario. If it is purely computational, use the for loop directly. If there are other IO operations, consider the parallel stream, which mainly depends on the cost of computing and thread synchronization

cancel comment

In fact, lambda still runs in a thread, so how to implement it?

Think of threads as a task pool. To create a task each time is to insert a task into the task pool. The program continues to move down until all the current code has been executed, and then obtains the next task execution from the thread.

Lambda plays this little trick, which makes people feel asynchronous. In fact, it is a fragmented operation of tasks.

The performance difference between a loop and a lambda depends on the task scheduling speed, but I think that in most cases, the for loop is more efficient.

The main advantage of lambda is that it is not easy to block.

cancel comment

According to the test results of Alex Zhitnitsky, sticking to the traditional Java programming style - iterator and for each loop - has better performance than the implementation of Java 8.

Using iterators or for each loops is the most effective way to traverse ArrayList, and its performance is twice as good as the traditional for loop with index values.

Among the methods of Java 8, the performance of parallel streams is the best. Be careful, however, that in some cases it may cause the program to run slower. Lambda expressions have a speed between stream and parallel stream.

cancel comment

No, after stream is enabled for parallel, the amount of large data will increase

cancel comment

No, the simpler the syntax, the slower the execution. This also explains why python is not as efficient as java

cancel comment

There are parallel streams in lambda, which can be multithreaded. However, for will still be faster if only one million pieces of data are traversed. When the data volume is small, multithreading may not be faster than single thread. However, when the data volume is large, tens of millions or even hundreds of millions, the parallel stream may be faster

cancel comment

Lambda and stream are two different things. The problem should be the comparison between stream and for loops. Normal serial stream is 1.4-1.5 times slower than for; The parallel stream loop will be faster and only suitable for large amount of data calculation. Otherwise, the thread overhead and synchronization operation will suffer performance loss. The running speed is about 0.6-0.8 times of the for loop

cancel comment
Load all 10 answers Loading
ZOL Q&A > UTM > Jinshan KingGate UTM > Jinshan KingGate UTM 100 > 1 million data traversal, lambda will be better than

Related products

  •  Jinshan KingGate UTM 100

    Jinshan KingGate UTM 100

    Reference price: ¥ 39800 Score:-

    Hot ranking list: not listed

  • Jingdong Search

report

Thank you for contributing to the harmony of the community. Please select the type of report

Reported successfully

It will be handled after verification
Thank you for your contribution to community harmony

 Is lambda faster than for loop when 1 million pieces of data traverse?

Click to locate the position of illegal characters

Scan the code to participate in the trial of new products at 0 yuan
You can get the sheets and grand gifts on the top floor

 Scan and pay attention to us
Tips

Are you sure you want to cancel this registration and exit this activity?