Google Translation API在List上表现不佳。如何提高?

时间:2018-04-26 20:09:00

标签: java performance google-translate

我正在使用google translate api翻译字符串列表,在这种情况下 - 糖果及其各自的颜色。我没有翻译的问题,但是,处理时间非常慢 - 大约20秒。我希望提高速度。任何建议表示赞赏,谢谢。我的代码如下:

这是我的Spring应用程序的服务层。处理完毕后,我将List<String>传递给我的REST服务中的控制器层。

我正在处理的数据集大约有100个元素。

public List<String> getSortedCandies() {
    List<Candy> candyList = new ArrayList<>(this.candyList);

    //sort candies
    candyList = candyList.stream()
            .sorted((o1, o2) -> {
                int nameResult = o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
                if (nameResult == 0) {
                    return o1.getColor().toUpperCase().compareTo(o2.getColor().toUpperCase());
                }
                return nameResult;
            }).collect(Collectors.toList());

    Translate translate = TranslateOptions.getDefaultInstance().getService();

    List<String> translatedList = new ArrayList<>();

    //translate from english to spanish
    for (Candy candy : candyList) {
        Detection detection = translate.detect(candy.toString());
        String detectedLanguage = detection.getLanguage();
        Translation translation = translate.translate(
                candy.toString(),
                Translate.TranslateOption.sourceLanguage(detectedLanguage),
                Translate.TranslateOption.targetLanguage("es")
        );
        translatedList.add(translation.getTranslatedText());
    }

    return translatedList;
}

1 个答案:

答案 0 :(得分:0)

您可以修改代码以使用parallelStream()Collections.synchronizedList()

public class StackOverflowSample {

    private List<Candy> candyList = new ArrayList<>();

    public StackOverflowSample() {

    }

    public StackOverflowSample(List<Candy> candyList) {
        this.candyList = candyList;
    }

    public List<String> getSortedCandies() {

        // sort candies
        candyList = candyList.stream().sorted((o1, o2) -> {
            int nameResult = o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
            if (nameResult == 0) {
                return o1.getColor().toUpperCase().compareTo(o2.getColor().toUpperCase());
            }
            return nameResult;
        }).collect(Collectors.toList());

        Translate translate = TranslateOptions.getDefaultInstance().getService();

        List<String> translatedList = Collections.synchronizedList(new LinkedList<>());

        candyList.parallelStream().forEach(c -> {
            Detection detection = translate.detect(c.toString());
            String detectedLanguage = detection.getLanguage();
            Translation translation = translate.translate(c.toString(),
                    Translate.TranslateOption.sourceLanguage(detectedLanguage),
                    Translate.TranslateOption.targetLanguage("es"));
            translatedList.add(translation.getTranslatedText());
        });

        return translatedList;
    }
}

我已经对代码进行了测试,但也许您可以按照此答案中的建议获取流程前后的当前时间进行比较:

https://stackoverflow.com/a/1770475/9015852